【问题标题】:Adding Labels other then ItemsControl Binding添加除 ItemsControl 绑定之外的标签
【发布时间】: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中,与这些Horizo​​ntal 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


【解决方案1】:

您可以使用 GridSharedSize 实现此目的:

<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 属性设置为 trueSharedSizeGroup。 p>

Share Sizing Properties Between Grids

【讨论】:

  • 谢谢 :-) 正是我要找的东西。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
  • 2012-12-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多