[英]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.AllActive
的ContainerViewModel
和一个继承了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
(通过提供Position
的event
在run-time
删除和添加Items
)。
I would like to switch to use a Grid
with a predefined number of Columns
and Rows
as below: 我想切换为使用具有以下预定义数量的
Columns
和Rows
的Grid
:
+------------------------------------------------------+
| 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 Index
和Column Index
下方的Position
和Points
\\ 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
需要具有ColumnIndex
和RowIndex
属性的概念才能插入。 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)
通过
ContainerViewModel
到PanelViewModel
作为parent
领域,并得到PanelViewModel
通过电话回parent
打电话的时候干将ColumnIndex
和RowIndex
的属性通过将自身作为参数来调用,即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
,但它可以实现您在ColumnIndex
和RowIndex
所在的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.