enum displayname descriptions description c# wpf

displayname - enums description c#



Icono de enlace dependiendo de Enum en WPF Treeview (3)

Crear un convertidor de valor

Toma su valor de enumeración y devuelve el nombre de archivo del icono apropiado.

[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(); } }

Deberá agregar una referencia a este convertidor en su XAML

<Window ... xmlns:converters="clr-namespace:App.Converters" ...> <Window.Resources> <converters:AcceptationStatusGlobalFlagToIconFilenameConverter x:Key="IconConverter"/> </Window.Resources>

Reemplace su TextBlock

<TextBlock TextAlignment="Justify" VerticalAlignment="Center" Text="{Binding Path=AcceptationStatusGlobalFlag}" />

Con una imagen y dile que use tu convertidor.

<Image Source="{Binding AcceptationStatusGlobalFlag, Converter={StaticResource IconConverter}}"/>

Tengo en Treeview TextBox, y quiero convertir mi Enum:

<TextBlock TextAlignment="Justify" VerticalAlignment="Center" Text="{Binding Path=AcceptationStatusGlobalFlag}" /> public enum AcceptationStatusGlobalFlag { NotReady = 0, Ready = 1, AcceptedByAdmin=2 }

a los iconos. Habrá 3 iconos, digamos ready.jpg, notready.jpg y AcceptedByAdmin.jpg

El país y la región tienen pool AcceptationStatusGlobalFlag y en ambos quiero mostrar este enum / Icono

<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>