答案 0 :(得分:3)
构建一个RoomViewModel
类,公开Description
,IsAvailable
,OtherInformation
以及其他属性并实现INotifyPropertyChanged
。如何填充这些属性取决于您的应用程序。
构建一个RoomsViewModel
类,公开名为ObservableCollection<RoomViewModel>
的{{1}}。
为Rooms
和DataTemplate
课程创建RoomViewModel
(见下文)。
创建RoomsViewModel
类的实例并填充其RoomsViewModel
集合。
创建Rooms
并将其ContentPresenter
属性设置为Content
类的实例。
典型的数据模板可能如下所示:
RoomsViewModel
未来的改进:
尝试使用<DataTemplate x:Type="{local:RoomsViewModel}">
<ItemsControl ItemsSource="{Binding Rooms}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</DataTemplate>
<DataTemplate x:Type="{local:RoomViewModel}">
<Button
Margin="10"
IsEnabled="{Binding IsAvailable}"
ToolTip="{Binding OtherInformation}"
Content="{Binding Description}"/>
</DataTemplate>
代替UniformGrid
。
阅读Josh Smith的文章Using RoutedCommands with a ViewModel in WPF并使用其中描述的技术在WrapPanel
上创建ReserveRoomCommand
属性。将RoomViewModel
数据模板中的CommandBinding
设置为RoomViewModel
。请注意,执行此操作后,您将删除绑定到{Binding ReserveRoomCommand}
,因为命令绑定将自动启用和禁用该按钮。
如果您需要重复使用此用户界面,请将数据模板和内容展示者移至IsEnabled
。