如何在 wpf 中放大 window 时拉伸我的(未知数量的)标签?
How to stretch my (unknown number of) labels when the window is enlarged in wpf?
作业
在可调整大小的 window 上创建未知数量的带有 ItemsControl
的标签。 Labels 应该在 ItemsControl
.
的右侧 offSet
问题
当我的标签重新调整为更大的格式时,我很难找到一种方法来拉伸 window,同时保持它们彼此的偏移。我的绑定工作完美。偏移量也正常工作,但现在我需要标签在重新调整 window 大小时同时保持与 Canvas.Left
.
的相对距离
代码
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<ItemsControl Grid.Row="0" ItemsSource="{Binding Path=Labels}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas IsItemsHost="True">
<Canvas.Background>
<SolidColorBrush Color="Black"/>
</Canvas.Background>
</Canvas>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Canvas.Left" Value="{Binding Path=OffSet}"/>
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Label Foreground="White" HorizontalAlignment="Left" Content="{Binding Path=Name}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
我找不到解决办法,试了很多。有什么建议吗?
正如我在评论中所建议的,我认为在这种情况下使用更好的面板可能是 UniformGrid
。只要你能在代码中计算出你的学位值是什么,你就可以制作一个你可以绑定的集合。
XAML:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ItemsControl ItemsSource="{Binding Degrees}" VerticalAlignment="Top">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Rows="1" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Black" BorderThickness="1" Margin="2">
<TextBlock Text="{Binding}" TextAlignment="Center" />
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Window>
代码隐藏:
using System.Linq;
using System.Windows;
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Degrees = Enumerable.Range(1, 5).Select(x => x * 10 + 100).ToArray();
DataContext = this;
}
public int[] Degrees { get; private set; }
}
}
正如您在调整 window 大小时看到的那样,各个元素会调整大小以占据适当的宽度。
我认为这是您想要的类型,但如果不是,请发表评论,我会努力改进我的答案。
作业
在可调整大小的 window 上创建未知数量的带有 ItemsControl
的标签。 Labels 应该在 ItemsControl
.
问题
当我的标签重新调整为更大的格式时,我很难找到一种方法来拉伸 window,同时保持它们彼此的偏移。我的绑定工作完美。偏移量也正常工作,但现在我需要标签在重新调整 window 大小时同时保持与 Canvas.Left
.
代码
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<ItemsControl Grid.Row="0" ItemsSource="{Binding Path=Labels}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas IsItemsHost="True">
<Canvas.Background>
<SolidColorBrush Color="Black"/>
</Canvas.Background>
</Canvas>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Canvas.Left" Value="{Binding Path=OffSet}"/>
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Label Foreground="White" HorizontalAlignment="Left" Content="{Binding Path=Name}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
我找不到解决办法,试了很多。有什么建议吗?
正如我在评论中所建议的,我认为在这种情况下使用更好的面板可能是 UniformGrid
。只要你能在代码中计算出你的学位值是什么,你就可以制作一个你可以绑定的集合。
XAML:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ItemsControl ItemsSource="{Binding Degrees}" VerticalAlignment="Top">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Rows="1" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Black" BorderThickness="1" Margin="2">
<TextBlock Text="{Binding}" TextAlignment="Center" />
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Window>
代码隐藏:
using System.Linq;
using System.Windows;
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Degrees = Enumerable.Range(1, 5).Select(x => x * 10 + 100).ToArray();
DataContext = this;
}
public int[] Degrees { get; private set; }
}
}
正如您在调整 window 大小时看到的那样,各个元素会调整大小以占据适当的宽度。
我认为这是您想要的类型,但如果不是,请发表评论,我会努力改进我的答案。