【发布时间】:2018-04-18 07:58:29
【问题描述】:
我有一个名为Channel的Model,由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
现在我需要添加一些Label,表示这些Quantities的描述,比如30代表Minutes,False代表Set或Not等。这些label会在另一个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>
【问题讨论】:
-
@Clemens 但这将在每个记录中创建两列。不是吗?除了所有现有的列之外,我只想多一列。
-
啊,好的。没有意识到这一点。对齐以哪种方式失败?由于有所有标签元素,它应该可以工作。
-
@Clemens 我不确定。但我觉得它可能不是与 ItemSource 相邻的固定 StackPanel 的正确方法,它实际上代表那些记录
-
如果感觉更好,您可以将带有附加标签的 StackPanel 放入 ItemsControl 的 ControlTemplate 中,就在 ItemsPresenter 之前。
-
@Clemens Amm 好的。你能指导一下,如何做到这一点?
标签: c# wpf xaml binding itemscontrol