我有 treeview 文本框,我想转换我的枚举:
<TextBlock TextAlignment="Justify" VerticalAlignment="Center" Text="{Binding Path=AcceptationStatusGlobalFlag}" />
public enum AcceptationStatusGlobalFlag
{
NotReady = 0,
Ready = 1,
AcceptedByAdmin=2
}
到图标。将有 3 个图标,比如说 ready.jpg、notready.jpg 和 AcceptedByAdmin.jpg
国家和地区有池 AcceptationStatusGlobalFlag 并且我想在两者上显示这个枚举/图标
<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>
最佳答案
创建一个 Value Converter
它获取您的枚举值并返回相应图标的文件名。
[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();
}
}
您需要在 XAML 中添加对此转换器的引用
<Window ... xmlns:converters="clr-namespace:App.Converters" ...>
<Window.Resources>
<converters:AcceptationStatusGlobalFlagToIconFilenameConverter x:Key="IconConverter"/>
</Window.Resources>
替换你的文本 block
<TextBlock TextAlignment="Justify" VerticalAlignment="Center" Text="{Binding Path=AcceptationStatusGlobalFlag}" />
使用图像并告诉它使用你的转换器
<Image Source="{Binding AcceptationStatusGlobalFlag, Converter={StaticResource IconConverter}}"/>
关于c# - 根据 WPF Treeview 中的枚举绑定(bind)图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2786792/