简体   繁体   English

如何使用MVM模式在WPF中创建按钮矩阵

[英]How to create a button matrix in wpf with mvvm pattern

I need to create a datagrid that look like a matrix, 我需要创建一个看起来像矩阵的数据网格,
The matrix display buttons for each day and its 24 hours. 矩阵显示每天及其24小时的按钮。
somthing like: 像这样的东西:
7 6 5 4 3 2 1 7 6 5 4 3 2 1
0 0
1 1个
.
.
23 23
I am using MVVM pattern that seems to make it harder to implement, 我正在使用MVVM模式,该模式似乎很难实现,
thank you all. 谢谢你们。

Something like this: 像这样:

<ItemsControl ItemsSource="{Binding DaysOfWeek}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid Rows="1"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <ItemsControl ItemsSource="{Binding HoursInDay}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <UniformGrid Columns="1"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

This assumes you have a collection called DaysOfWeek in your data context (ie. in your main view model). 假设您在数据上下文中(即在主视图模型中)有一个名为DaysOfWeek的集合。 Each DayOfWeek object would expose an HoursInDay collection (likely the same shared collection). 每个DayOfWeek对象都会公开HoursInDay集合(可能是相同的共享集合)。

Having said all that, what is the advantage to data-driving this? 说了这么多,数据驱动这有什么好处? Is your matrix likely to change in dimensions? 您的矩阵尺寸可能会改变吗? If not, why not just "hard-code" the matrix in your view? 如果没有,为什么不只是在视图中“硬编码”矩阵? Each matrix cell can still bind to an appropriate data item in your view model. 每个矩阵单元仍可以绑定到视图模型中的适当数据项。

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

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