如何将ItemsControl(网格外)绑定到网格?

时间:2017-06-30 10:08:40

标签: c# wpf xaml grid

我有一个viewmodel,它包含对象名称MyLabel的ObservableCollection。这些对象有3个属性(content,rowNr,columnNr),它们应分别绑定到Content,Grid.Row和Grid.Column属性。

我定义ItemsControl的原因是因为它在我的网格中没有工作,也就是说我无法绑定rowNr和columnNr,因为网格Grid.Column /Grid.Row属性会一直覆盖我的数据。

如何使这项工作成为我的标签在网格内?

<StackPanel>
    <ItemsControl ItemsSource="{Binding Path=MyLabelList}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Label Content="{Binding content}" Grid.Column="{Binding columnNr}" Grid.Row="{Binding rowNr}" Style="{StaticResource MyLabel}" />
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
    </Grid>
</StackPanel>

4 个答案:

答案 0 :(得分:1)

尝试使用Grid作为ItemsPanel的{​​{1}}:

ItemsControl

答案 1 :(得分:1)

您必须在[{1}}

上以编程方式将绑定设置为ItemContainer
GetContainerForItemOverride()

在这样的视图中:

public class GridItemsControl : ItemsControl
{
    protected override DependencyObject GetContainerForItemOverride()
    {
        FrameworkElement fwE = base.GetContainerForItemOverride() as FrameworkElement;
        fwE.SetBinding(Grid.ColumnProperty, "columnNr");
        fwE.SetBinding(Grid.RowProperty, "rowNr");

        return fwE;
    }
}

您还可以实现一个自己的网格,生成所需的行数和列数。

这个技巧非常合法。

其他信息 您的<custom:GridItemsControl> <custom:GridItemsControl.ItemsPanel> <ItemsPanelTemplate> <Grid> <Grid.ColumnDefinitions> <!-- Add here --> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <!-- Add here --> </Grid.RowDefinitions> </Grid> </ItemsPanelTemplate> </custom:GridItemsControl.ItemsPanel> </custom:GridItemsControl> 不会直接添加到ItemTemplate(在您的情况下为ItemsPanel),总共有Grid或类似的内容,因为您正在使用ContentPresenter 1}}。所以你必须这样。

为了让它更具有合法性,你可以想象它是这样的:

DataTemplate

答案 2 :(得分:1)

GridItemsControl不能很好地协同播放,但您可以参考以下博文:

使用网格作为ItemsControl的面板: http://blog.scottlogic.com/2010/11/15/using-a-grid-as-the-panel-for-an-itemscontrol.html

另一种选择可能是&#34;手动&#34;在视图中动态地将<RowDefinition><ColumnDefinition>元素添加到网格中。

答案 3 :(得分:1)

如果您不希望像答案中建议的@Peter那样继承ItemsControl,则只需使用ItemsControl.ItemContainerStyle绑定Grid.ColumnGrid.Row属性:

<ItemsControl ItemsSource="{Binding Path=MyLabelList}">
    <ItemsPanelTemplate>
        <Grid>
            <Grid.ColumnDefinitions>
                <!-- Add here -->
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <!-- Add here -->
            </Grid.RowDefinitions>
        </Grid>
    </ItemsPanelTemplate>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Label Content="{Binding content}" Style="{StaticResource MyLabel}" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
    <ItemsControl.ItemContainerStyle>
        <Style TargetType="ContentPresenter">
            <Setter Property="Grid.Row" Value="{Binding rowNr}" />
            <Setter Property="Grid.Column" Value="{Binding columnNr}" />
        </Style>
    </ItemsControl.ItemContainerStyle>
</ItemsControl>

但是,正如其他人已经提到的那样,列和行定义不会自动添加,您需要在设计时知道它并静态添加它们,或者您需要找到某种方式在运行时动态添加它们。

相关问题