简体   繁体   English

绑定到网格的特定行和列

[英]Binding to specific rows and columns of a Grid

I have a ContainerViewModel which inherits Conductor<T>.Collection.AllActive and a PanelViewModel which inherits Screen and whose lifetime is controlled by ContainerViewModel 我有一个继承了Conductor<T>.Collection.AllActiveContainerViewModel和一个继承了Screen且其生命周期由ContainerViewModel控制的PanelViewModel

My ContainerView uses a templated ItemsControl to render a my Collection<T> managed by the conductor. 我的ContainerView使用templated ItemsControl呈现由导体管理的我的Collection<T> This view currently uses a UniformGrid control and the items are displayed in the order for which they are added to the Conductor<T>.Collection ( Items are removed and added at run-time via an event which provides a Position ). 此视图当前使用UniformGrid控件,并且按照将它们添加到Conductor<T>.Collection UniformGrid的顺序显示items (通过提供Positioneventrun-time删除和添加Items )。

I would like to switch to use a Grid with a predefined number of Columns and Rows as below: 我想切换为使用具有以下预定义数量的ColumnsRowsGrid

+------------------------------------------------------+
|      1     |      2      |      3      |      4      |
|     0,0    |     0,1     |     0,2     |     0,3     |
+------------------------------------------------------+
|      5     |      6      |      7      |      8      |
|     1,0    |     1,1     |     1,2     |     1,3     |
+------------------------------------------------------+

The single number represents a Position and the Points \\ Coordinates underneath the Row Index and Column Index . 单个数字表示Row IndexColumn Index下方的PositionPoints \\ Coordinates

I know I could do something like the following: 我知道我可以做以下事情:

<ItemsControl ItemsSource="{Binding Items}">
    <!-- ItemsPanelTemplate -->
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition />
                    <ColumnDefinition />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
            </Grid>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

    <!-- ItemContainerStyle -->
    <ItemsControl.ItemContainerStyle>
        <Style>
            <Setter Property="Grid.Column"
                    Value="{Binding ColumnIndex}" />
            <Setter Property="Grid.Row"
                    Value="{Binding RowIndex}" />
        </Style>
    </ItemsControl.ItemContainerStyle>

    <!-- ItemTemplate -->
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <ContentControl Content="{Binding}" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

My issue with the above is that the PanelViewModel to be displayed as the Content needs a concept of the ColumnIndex and RowIndex properties for this to hook into. 上面的问题是,要作为Content显示的PanelViewModel需要具有ColumnIndexRowIndex属性的概念才能插入。 I'm not entirely happy with this as I see it being the responsibility of ContainerViewModel to manage the positioning. 我对此并不完全满意,因为我认为这是ContainerViewModel管理位置的责任。

Is there a way I can achieve this where only the ContainerViewModel is aware of the Position concept? 如果只有ContainerViewModel知道Position概念,有什么方法可以实现?

Pass ContainerViewModel to the PanelViewModel as a parent field and get PanelViewModel to pass the call back to parent when calling the getters of ColumnIndex and RowIndex properties by passing itself as the parameter to that call ie parent.GetColumnIndex(this) and parent.GetRowIndex(this) 通过ContainerViewModelPanelViewModel作为parent领域,并得到PanelViewModel通过电话回parent打电话的时候干将ColumnIndexRowIndex的属性通过将自身作为参数来调用,即parent.GetColumnIndex(this)parent.GetRowIndex(this)

While this means PanelViewModel now knows about ContainerViewModel , it achieves what you were looking for where the ColumnIndex and RowIndex , while properties of the PanelViewModel are implemented in ContainerViewModel . 虽然这意味着PanelViewModel现在了解ContainerViewModel ,但它可以实现您在ColumnIndexRowIndex所在的RowIndex ,而PanelViewModel属性是在ContainerViewModel中实现的。

You could go further and abstract the method calls out to an interface so that the parent field is of the interface type rather than of type ContainerViewModel - it depends on how likely it is that PanelViewModel will be contained by other types. 您可以进一步将方法调用抽象到接口,以便父字段属于接口类型,而不是ContainerViewModel类型-这取决于其他类型包含PanelViewModel可能性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM