Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 绑定到网格的特定行和列_C#_Wpf_Xaml_Caliburn.micro - Fatal编程技术网

C# 绑定到网格的特定行和列

C# 绑定到网格的特定行和列,c#,wpf,xaml,caliburn.micro,C#,Wpf,Xaml,Caliburn.micro,我有一个ContainerViewModel继承了Conductor.Collection.AllActive和一个PanelViewModel继承了Screen,其生存期由ContainerViewModel控制 MyContainerView使用模板化的项控件呈现由售票员管理的My集合。此视图当前使用UniformGrid控件,项按其添加到导体的顺序显示。集合(项通过提供位置的事件在运行时删除和添加) 我想切换到使用具有预定义数量的列和行的网格,如下所示: +----------------

我有一个
ContainerViewModel
继承了
Conductor.Collection.AllActive
和一个
PanelViewModel
继承了
Screen
,其生存期由
ContainerViewModel
控制

My
ContainerView
使用模板化的
项控件
呈现由售票员管理的My
集合。此视图当前使用
UniformGrid
控件,
按其添加到
导体的顺序显示。集合
通过提供
位置的
事件在
运行时
删除和添加)

我想切换到使用具有预定义数量的
网格
,如下所示:

+------------------------------------------------------+
|      1     |      2      |      3      |      4      |
|     0,0    |     0,1     |     0,2     |     0,3     |
+------------------------------------------------------+
|      5     |      6      |      7      |      8      |
|     1,0    |     1,1     |     1,2     |     1,3     |
+------------------------------------------------------+
单个数字表示
位置
行索引和
列索引下的
\
坐标

我知道我可以做如下事情:

<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
需要
ColumnIndex
RowIndex
属性的概念,以便将其挂接。我对此并不完全满意,因为我认为管理定位是
ContainerViewModel
的责任


如果只有
ContainerViewModel
知道
位置
概念,我有没有办法做到这一点?

ContainerViewModel
传递到
PanelViewModel
作为
parent
字段,并在调用
ColumnIndex
RowIndex
属性,方法是将自身作为参数传递给该调用,即
parent.GetColumnIndex(this)
parent.GetRowIndex(this)

虽然这意味着
PanelViewModel
现在知道了
ContainerViewModel
,但它实现了您所寻找的
列索引和
行索引的位置,而
PanelViewModel
的属性是在
ContainerViewModel
中实现的

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