簡體   English   中英

嵌套項控制將數據綁定到同一網格

[英]Nested Itemscontrols data binding to the same Grid

這可能是一個騙子,但是我在一個小時左右的搜索和閱讀MSDN中找不到任何東西。

我正在制作一個基於2D瓷磚的游戲,並且有一個ItemsControl綁定到Tiles列表(C#類)。 這很好。

<ItemsControl ItemsSource="{Binding Tiles}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid Rows="{Binding Board.Rows}" Columns="{Binding Board.Columns}"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.Resources>
        <DataTemplate DataType="{x:Type local:Tile}">
            <Grid x:Name="tileGrid" Background="{Binding TileImage}"/>
        </DataTemplate>
    </ItemsControl.Resources>
<ItemsControl>

每個圖塊都有一個內部5x5網格,因為我需要能夠將單元放置在每個圖塊的各個位置。 每個Tile包含一個List<Location> ,其中有一個要放置在該網格上的單元。

<Grid x:Name="tileGrid" Background="{Binding TileImage}">
    <ColumnDefinition snipped - there's 5 columns and 5 rows>
    <ItemsControl ItemsSource="{Binding Locations}">
        <ItemsControl.Resources>
            <DataTemplate DataType="{x:Type sys:Int32}">
                <Image Source="unit.bmp"
                        Grid.Column="{Binding Converter={StaticResource ColumnConverter}}"
                        Grid.Row="{Binding Converter={StaticResource RowConverter}}"/>
            </DataTemplate>
        </ItemsControl.Resources>
    </ItemsControl>

其中ColumnConverter和RowConverter只是從位置獲取列索引。

問題是我不能從ItemsControl內綁定到tileGrid的Column和Row。 有人建議將Grid放置在Locations ItemsControl內,但這不起作用,因為它必須是外部Tiles ItemsControl的子代。

我能做什么?

似乎我在重復以前的建議,但是您應該將tileGrid用作Locations ItemsControl的ItemsPanel,並使此ItemsControl成為Tiles ItemsControl的子代。 我不明白為什么這是不可能的?

也許您需要對DataTemplate中的tileGrid做一些事情。 因此,我認為有可能對ItemsControl做同樣的事情。

我還可以假設tileGrid中還有其他的ColumnDefinitions或RowDefinitions。 這樣,您可以在Locations中使用另一個Grid,並使用IsSharedSizeScopeSharedSizeGroup將其定義與tileGrid綁定

在ItemsControl.ItemContainerStyle中而不是在DataTemplate中設置Grid.Column和Grid.Row。 每個項目都包裝在ContentPresenter中,因此Grid.Column和Grid.Row無法正確應用

暫無
暫無

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

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