【问题标题】:Custom ItemsControl with grid layout具有网格布局的自定义 ItemsControl
【发布时间】:2011-12-17 17:17:37
【问题描述】:

我有用于显示数独游戏网格的自定义项目控件。我希望它将其项目显示为 9X9 网格。每个项目都有 X 和 Y 属性,我想绑定到网格中的该属性位置(网格行和网格。列)。除了绑定这些 grid.row 和 grid.column 属性外,一切看起来都正常。代码如下。这不是绑定的错,因为如果我使用硬值,什么都不会改变。请帮忙:

<ItemsControl  Margin="4" ItemsSource="{Binding Cells, Mode=OneWay}" x:Name="grid">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
                <grid:GridCell Grid.Column="{Binding X}" Grid.Row="{Binding Y}"  />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
                <Grid IsItemsHost="True" Background="Pink">
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition Height="2" />
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition Height="2" />
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition />
                        <ColumnDefinition />
                        <ColumnDefinition Width="2" />
                        <ColumnDefinition />
                        <ColumnDefinition />
                        <ColumnDefinition />
                        <ColumnDefinition Width="2" />
                        <ColumnDefinition />
                        <ColumnDefinition />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
            </Grid>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

编辑: 如果我使用例如,没有任何变化

<ItemsControl.ItemTemplate>
    <DataTemplate>
            <grid:GridCell Grid.Column="2" Grid.Row="2"  />
    </DataTemplate>
</ItemsControl.ItemTemplate>

【问题讨论】:

    标签: c# .net wpf xaml data-binding


    【解决方案1】:

    这些值被忽略,因为单元格不是Grid 的直接子级,它们被包裹在由ItemContainerGeneratorItemContainerGenerator 创建的ContentPresenter 中。

    您需要使用ItemContainerStyle 应用更高级别的值。

    <ItemsControl.ItemContainerStyle>
        <Style TargetType="{x:Type ContentPresenter}">
            <Setter Property="Grid.Column" Value="{Binding X}" />
            <Setter Property="Grid.Row" Value="{Binding Y}" />
        </Style>
    </ItemsControl.ItemContainerStyle>
    

    【讨论】:

      猜你喜欢
      • 2018-06-11
      • 2018-09-23
      • 1970-01-01
      • 1970-01-01
      • 2014-11-02
      • 2011-01-20
      • 2012-07-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多