添加 ItemsControl 绑定以外的标签
Adding Labels other then ItemsControl Binding
我有一个名为Channel的模型,由ChannelString、IsSet等一些属性组成,然后有一个Channel的ObservableCollection。
视图模型:
Channels = new ObservableCollection<Channel>();
PopulateChannels(Channels)
查看:
<ItemsControl ItemsSource="{Binding Path=Channels}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<Label Content="{Binding Path=ChannelString}" />
<Label Content="{Binding Path=IsSet}" />
<Label Content="{Binding Path=AlternationMinute}" />
<ComboBox ItemsSource="{Binding Path=DataContext.ProfileQuantities, RelativeSource={RelativeSource AncestorType=UserControl}}"
SelectedItem="{Binding Path=Profile1Id}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Converter={StaticResource Converter}}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
到目前为止,它运行良好,它看起来是这样的:
Profile View
现在我需要添加一些Labels,表示对这些Quantities的描述,比如30代表Minutes,False代表Set or Not等。这些labels会在另外一个Column中,与这些Horizontal相邻StackPanels,需要与现有组件对齐。
如果我按如下所示进行操作,这些将不会与 ItemsControl 中的项目对齐。那么实现这一目标的最佳方法是什么?
<StackPanel Orientation="Vertical">
<Label>Item Description 1</Label>
<Label>Item Description 2</Label>
<Label>Item Description 3</Label>
</StackPanel>
<ItemsControl ItemsSource="{Binding Path=Channels}">
</ItemsControl>
您可以使用 Grid 和 SharedSize:
<StackPanel Grid.IsSharedSizeScope="True" Orientation="Horizontal">
<Grid>
<Grid.RowDefinitions>
<RowDefinition SharedSizeGroup="FirstRow" />
<RowDefinition SharedSizeGroup="SecondRow" />
<RowDefinition SharedSizeGroup="ThirdRow" />
</Grid.RowDefinitions>
<Label Grid.Row="0">Item Description 1</Label>
<Label Grid.Row="1">Item Description 2</Label>
<Label Grid.Row="2">Item Description 3</Label>
</Grid>
<ItemsControl ItemsSource="{Binding Path=Channels}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition SharedSizeGroup="FirstRow" />
<RowDefinition SharedSizeGroup="SecondRow" />
<RowDefinition SharedSizeGroup="ThirdRow" />
<RowDefinition />
</Grid.RowDefinitions>
<Label Content="{Binding Path=ChannelString}" />
<Label Grid.Row="1" Content="{Binding Path=IsSet}" />
<Label Grid.Row="2" Content="{Binding Path=AlternationMinute}" />
<ComboBox Grid.Row="3" ItemsSource="{Binding Path=DataContext.ProfileQuantities, RelativeSource={RelativeSource AncestorType=UserControl}}"
SelectedItem="{Binding Path=Profile1Id}">
...
</ComboBox>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
不要忘记将 IsSharedSizeScope 属性 设置为 true 和 SharedSizeGroup 在您要与之共享高度的行上。
我有一个名为Channel的模型,由ChannelString、IsSet等一些属性组成,然后有一个Channel的ObservableCollection。
视图模型:
Channels = new ObservableCollection<Channel>();
PopulateChannels(Channels)
查看:
<ItemsControl ItemsSource="{Binding Path=Channels}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<Label Content="{Binding Path=ChannelString}" />
<Label Content="{Binding Path=IsSet}" />
<Label Content="{Binding Path=AlternationMinute}" />
<ComboBox ItemsSource="{Binding Path=DataContext.ProfileQuantities, RelativeSource={RelativeSource AncestorType=UserControl}}"
SelectedItem="{Binding Path=Profile1Id}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Converter={StaticResource Converter}}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
到目前为止,它运行良好,它看起来是这样的: Profile View
现在我需要添加一些Labels,表示对这些Quantities的描述,比如30代表Minutes,False代表Set or Not等。这些labels会在另外一个Column中,与这些Horizontal相邻StackPanels,需要与现有组件对齐。
如果我按如下所示进行操作,这些将不会与 ItemsControl 中的项目对齐。那么实现这一目标的最佳方法是什么?
<StackPanel Orientation="Vertical">
<Label>Item Description 1</Label>
<Label>Item Description 2</Label>
<Label>Item Description 3</Label>
</StackPanel>
<ItemsControl ItemsSource="{Binding Path=Channels}">
</ItemsControl>
您可以使用 Grid 和 SharedSize:
<StackPanel Grid.IsSharedSizeScope="True" Orientation="Horizontal">
<Grid>
<Grid.RowDefinitions>
<RowDefinition SharedSizeGroup="FirstRow" />
<RowDefinition SharedSizeGroup="SecondRow" />
<RowDefinition SharedSizeGroup="ThirdRow" />
</Grid.RowDefinitions>
<Label Grid.Row="0">Item Description 1</Label>
<Label Grid.Row="1">Item Description 2</Label>
<Label Grid.Row="2">Item Description 3</Label>
</Grid>
<ItemsControl ItemsSource="{Binding Path=Channels}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition SharedSizeGroup="FirstRow" />
<RowDefinition SharedSizeGroup="SecondRow" />
<RowDefinition SharedSizeGroup="ThirdRow" />
<RowDefinition />
</Grid.RowDefinitions>
<Label Content="{Binding Path=ChannelString}" />
<Label Grid.Row="1" Content="{Binding Path=IsSet}" />
<Label Grid.Row="2" Content="{Binding Path=AlternationMinute}" />
<ComboBox Grid.Row="3" ItemsSource="{Binding Path=DataContext.ProfileQuantities, RelativeSource={RelativeSource AncestorType=UserControl}}"
SelectedItem="{Binding Path=Profile1Id}">
...
</ComboBox>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
不要忘记将 IsSharedSizeScope 属性 设置为 true 和 SharedSizeGroup 在您要与之共享高度的行上。