简体   繁体   中英

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.
somthing like:
7 6 5 4 3 2 1
0
1
.
.
23
I am using MVVM pattern that seems to make it harder to implement,
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). Each DayOfWeek object would expose an HoursInDay collection (likely the same shared collection).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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