Hi John,
The following are two ways to get what you want. Firstly, the business class is defined as follows:
public class TestObj : INotifyPropertyChanged
{
private DataStateType _state;
public DataStateType EmailState
{
get { return _state; }
set {
if (value != _state)
{
_state = value;
OnPropertyChanged("EmailState");
}
}
}
private void OnPropertyChanged(string propname)
{
if (_propertyChanged != null)
{
_propertyChanged(this, new PropertyChangedEventArgs(propname));
}
}
private event PropertyChangedEventHandler _propertyChanged;
#region INotifyPropertyChanged Members
event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged
{
add { _propertyChanged += value; }
remove { _propertyChanged -= value; }
}
#endregion
}
Sample 1: Define a DataTemplate for eachvalueofDataStateType.UseaDataTemplateSelectorto select a properDataTemplateaccording to the value of theEmailState propertyof thebusiness object.
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="Window1" Height="300" Width="300">
<Wndow.Resources>
<local:TestObj x:Key="testobj" EmailState="OK"/>
<DataTemplate x:Key="OK" DataType="{x:Type local:DataStateType}">
<Image>
<Image.Source>
<BitmapImage UriSource="Images/Bitmap1.bmp"/>
</Image.Source>
</Image>
</DataTemplate>
<DataTemplate x:Key="Error" DataType="{x:Type local:DataStateType}">
<Image>
<Image.Source>
<BitmapImage UriSource="Images/Bitmap2.bmp"/>
</Image.Source>
</Image>
</DataTemplate>
<DataTemplate x:Key="SevereError" DataType="{x:Type local:DataStateType}">
<Image>
<Image.Source>
<BitmapImage UriSource="Images/Bitmap3.bmp"/>
</Image.Source>
</Image>
</DataTemplate>
<DataTemplate x:Key="Warning" DataType="{x:Type local:DataStateType}">
<Image>
<Image.Source>
<BitmapImage UriSource="Images/Bitmap4.bmp"/>
</Image.Source>
</Image>
</DataTemplate>
<local:DataStateTemplateSelector x:Key="templateSelector"/>
</Window.Resources>
<StackPanel >
<ContentControl DataContext="{StaticResource testobj}" Content="{Binding Path=EmailState}" Width="200" Height="40"
ContentTemplate="{StaticResource GeneralTemplate}">
</ContentControl>
</StackPanel>
</Window>
// code behind
public class DataStateTemplateSelector : DataTemplateSelector
{
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
if (item != null && item is DataStateType)
{
Window win = Application.Current.MainWindow;
switch((DataStateType)item)
{
case DataStateType.OK:
{
return win.FindResource("OK") as DataTemplate;
}
case DataStateType.Error:
{ return win.FindResource("Error") as DataTemplate; }
case DataStateType.SevereError:
{ return win.FindResource("SevereError") as DataTemplate; }
case DataStateType.Warning:
{ return win.FindResource("Warning") as DataTemplate; }
}
}
return null;
}
}
Sample 2: Define only one DataTemplate and use data triggers to show the proper image according to the value of the EmailState property.
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="Window1" Height="300" Width="300">
<Wndow.Resources>
<local:TestObj x:Key="testobj" EmailState="OK"/>
<DataTemplate x:Key="GeneralTemplate" DataType="{x:Type local:DataStateType}">
<Grid Name="grid">
<Image Name="OK" Visibility="Hidden">
<Image.Source>
<BitmapImage UriSource="Images/Bitmap1.bmp"/>
</Image.Source>
</Image>
<Image Name="Error" Visibility="Hidden" >
<Image.Source>
<BitmapImage UriSource="Images/Bitmap2.bmp"/>
</Image.Source>
</Image>
<Image Name="SevereError" Visibility="Hidden">
<Image.Source>
<BitmapImage UriSource="Images/Bitmap3.bmp"/>
</Image.Source>
</Image>
<Image Name="Warning" Visibility="Hidden">
<Image.Source>
<BitmapImage UriSource="Images/Bitmap4.bmp"/>
</Image.Source>
</Image>
</Grid>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Content}" Value="{x:Static local:DataStateType.OK}">
<Setter Property="Visibility" TargetName="OK" Value="Visible"/>
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Content}" Value="{x:Static local:DataStateType.Error}">
<Setter Property="Visibility" TargetName="Error" Value="Visible"/>
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Content}" Value="{x:Static local:DataStateType.SevereError}">
<Setter Property="Visibility" TargetName="SevereError" Value="Visible"/>
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Content}" Value="{x:Static local:DataStateType.Warning}">
<Setter Property="Visibility" TargetName="Warning" Value="Visible"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</Window.Resources>
<StackPanel >
<ContentControl DataContext="{StaticResource testobj}" Content="{Binding Path=EmailState}" Width="200" Height="40"
ContentTemplate="{StaticResource GeneralTemplate}">
</ContentControl>
</StackPanel>
</Window>
Hope this helps.
If you have anything unclear, please feel free to let me know.
Sincerely,
Linda Liu
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the
All-In-One Code Framework! If you have any feedback, please tell us.