well , seriously guys i got sick of WPF errors and hard handling , look i got many buttons are dsigned to represent rooms and i want to bind into a tooltip to get occupier name and informations from database . i cant find how to do it. Thanks
Build a
RoomViewModel
class that exposesDescription
,IsAvailable
,OtherInformation
, and other properties and implementsINotifyPropertyChanged
. How you populate these properties is up to your application.Build a
RoomsViewModel
class that exposes anObservableCollection<RoomViewModel>
namedRooms
.Create
DataTemplate
s for theRoomViewModel
andRoomsViewModel
classes (see below).Create an instance of the
RoomsViewModel
class and populate itsRooms
collection.Create a
ContentPresenter
and set itsContent
property to the instance of yourRoomsViewModel
class.
Typical data templates might look like this:
<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>
Future enhancements:
Try using a
UniformGrid
instead of aWrapPanel
.Read Josh Smith's article Using RoutedCommands with a ViewModel in WPF and use the techniques described there to create a
ReserveRoomCommand
property on theRoomViewModel
. Set theCommandBinding
in theRoomViewModel
data template to{Binding ReserveRoomCommand}
. Note that once you do this, you'll remove the binding toIsEnabled
, because the command binding will enable and disable the button automatically.If you are going to need to reuse this UI, move the data templates and content presenter into a
UserControl
.