簡體   English   中英

具有網格布局的自定義ItemsControl

[英]Custom ItemsControl with grid layout

我有用於顯示數獨游戲網格的自定義項控件。 我希望它顯示其項目到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>

忽略這些值是因為這些單元格不是Grid的直接子級,它們被包裝在由ItemsControlItemContainerGenerator創建的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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM