【问题标题】:Binding to specific rows and columns of a Grid绑定到 Grid 的特定行和列
【发布时间】:2014-11-24 17:01:17
【问题描述】:

我有一个 ContainerViewModel 继承 Conductor<T>.Collection.AllActive 和一个 PanelViewModel 继承 Screen 并且其生命周期由 ContainerViewModel 控制

我的ContainerView 使用templated ItemsControl 来呈现由指挥管理的我的Collection<T>。此视图当前使用UniformGrid 控件,items 按添加到Conductor<T>.Collection 的顺序显示(Items 被删除并通过event 添加到run-time,该event 提供@ 987654336@).

我想改用Grid,预定义数量为ColumnsRows,如下所示:

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

单个数字表示PositionPoints 下方的CoordinatesRow IndexColumn Index

我知道我可以执行以下操作:

<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>

我对上面的问题是 PanelViewModel 要显示为 Content 需要 ColumnIndexRowIndex 属性的概念才能挂钩。我对此并不完全满意,因为我认为管理定位是 ContainerViewModel 的责任。

有没有一种方法可以实现这一点,只有ContainerViewModel 知道Position 概念?

【问题讨论】:

    标签: c# wpf xaml caliburn.micro


    【解决方案1】:

    ContainerViewModel 作为parent 字段传递给PanelViewModel,并在调用ColumnIndexRowIndex 属性的getter 时,通过将自身作为该调用的参数,即parent.GetColumnIndex(this)parent.GetRowIndex(this)

    虽然这意味着PanelViewModel 现在知道ContainerViewModel,但它实现了您在ColumnIndexRowIndex 中寻找的内容,而PanelViewModel 的属性在ContainerViewModel 中实现。

    您可以更进一步,将调用的方法抽象为接口,以便父字段属于接口类型而不是ContainerViewModel 类型 - 这取决于PanelViewModel 被其他字段包含的可能性有多大类型。

    【讨论】:

      猜你喜欢
      • 2012-10-20
      • 2013-06-27
      • 2016-06-24
      • 2015-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多