I have at treeview TextBox, and I want convert my Enum:
<TextBlock TextAlignment="Justify" VerticalAlignment="Center" Text="{Binding Path=AcceptationStatusGlobalFlag}" />
public enum AcceptationStatusGlobalFlag
{
NotReady = 0,
Ready = 1,
AcceptedByAdmin=2
}
to Icons. There will be 3 icons, let say ready.jpg, notready.jpg and AcceptedByAdmin.jpg
Country and Region has pool AcceptationStatusGlobalFlag and on both I want to display this enum/Icon
<TreeView Name="structureTree" SelectedItemChanged="structureTree_SelectedItemChanged" Grid.Row="0" Grid.Column="0" ItemsSource="{Binding}" Height="413" ScrollViewer.VerticalScrollBarVisibility="Visible" ScrollViewer.HorizontalScrollBarVisibility="Visible" Width="Auto" PreviewMouseRightButtonUp="structureTree_PreviewMouseRightButtonUp" FontFamily="Verdana" FontSize="12">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type ServiceMy:Country}"
ItemsSource="{Binding Path=ListOfRegions}">
<StackPanel Orientation="Horizontal">
<TextBlock TextAlignment="Justify" VerticalAlignment="Center" Text="{Binding Path=Name}"/>
<TextBlock TextAlignment="Justify" VerticalAlignment="Center" Text=" H:"/>
<TextBlock TextAlignment="Justify" VerticalAlignment="Center" Text="{Binding Path=NumberOfHotels}"/>
<TextBlock TextAlignment="Justify" VerticalAlignment="Center" Text=" "/>
<TextBlock TextAlignment="Justify" VerticalAlignment="Center" Text=" FG:"/>
<TextBlock TextAlignment="Justify" VerticalAlignment="Center" Text="{Binding Path=AcceptationStatusGlobalFlag}" />
<!--<Button Name="BTNAddRegion" Height="20" Content="+" Click="BTNAddRegion_Click"></Button>-->
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type ServiceMy:Region}"
ItemsSource="{Binding Path=ListOfProvinces}">
<StackPanel Orientation="Horizontal">
<TextBlock TextAlignment="Justify" VerticalAlignment="Center" Text="{Binding Path=Name}"/>
<TextBlock TextAlignment="Justify" VerticalAlignment="Center" Text=" H:"/>
<TextBlock TextAlignment="Justify" VerticalAlignment="Center" Text="{Binding Path=NumberOfHotels}"/>
<TextBlock TextAlignment="Justify" VerticalAlignment="Center" Text=" "/>
<!--<Button Name="BTNAddProvince" Height="20" Content="+" Click="BTNAddProvince_Click"></Button>-->
</StackPanel>
</DataTemplate>
</TreeView.Resources>
</TreeView>
</GroupBox>
</StackPanel>
</Grid>
Create a Value Converter
It takes your enum value and returns the filename of the appropriate icon.
[ValueConversion(typeof(AcceptationStatusGlobalFlag), typeof(string))]
public class AcceptationStatusGlobalFlagToIconFilenameConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
switch ((AcceptationStatusGlobalFlag)value)
{
case AcceptationStatusGlobalFlag.Ready:
return "ready.jpg";
case AcceptationStatusGlobalFlag.NotReady:
return "notready.jpg";
case AcceptationStatusGlobalFlag.AcceptedByAdmin:
return "AcceptedByAdmin.jpg";
default:
return null;
}
// or
return Enum.GetName(typeof(AcceptationStatusGlobalFlag), value) + ".jpg";
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
}
You will need to add a reference to this converter in your XAML
<Window ... xmlns:converters="clr-namespace:App.Converters" ...>
<Window.Resources>
<converters:AcceptationStatusGlobalFlagToIconFilenameConverter x:Key="IconConverter"/>
</Window.Resources>
Replace your TextBlock
<TextBlock TextAlignment="Justify" VerticalAlignment="Center" Text="{Binding Path=AcceptationStatusGlobalFlag}" />
with an Image and tell it use your converter
<Image Source="{Binding AcceptationStatusGlobalFlag, Converter={StaticResource IconConverter}}"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With