海王星[刚来自火星]

时间:2010-08-10 16:10:33

标签: wpf

好吧,认真的家伙我厌倦了WPF错误和难以处理,看起来我有很多按钮被指定代表房间,我想绑定到工具提示以从数据库获取占用者名称和信息。 我无法找到如何做到这一点。 感谢

1 个答案:

答案 0 :(得分:3)

  1. 构建一个RoomViewModel类,公开DescriptionIsAvailableOtherInformation以及其他属性并实现INotifyPropertyChanged。如何填充这些属性取决于您的应用程序。

  2. 构建一个RoomsViewModel类,公开名为ObservableCollection<RoomViewModel>的{​​{1}}。

  3. RoomsDataTemplate课程创建RoomViewModel(见下文)。

  4. 创建RoomsViewModel类的实例并填充其RoomsViewModel集合。

  5. 创建Rooms并将其ContentPresenter属性设置为Content类的实例。

  6. 典型的数据模板可能如下所示:

    RoomsViewModel

    未来的改进:

    1. 尝试使用<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

    2. 阅读Josh Smith的文章Using RoutedCommands with a ViewModel in WPF并使用其中描述的技术在WrapPanel上创建ReserveRoomCommand属性。将RoomViewModel数据模板中的CommandBinding设置为RoomViewModel。请注意,执行此操作后,您将删除绑定到{Binding ReserveRoomCommand},因为命令绑定将自动启用和禁用该按钮。

    3. 如果您需要重复使用此用户界面,请将数据模板和内容展示者移至IsEnabled