根据枚举在WPF树视图绑定图标 [英] Bind Icon depending on Enum in WPF Treeview

查看:217
本文介绍了根据枚举在WPF树视图绑定图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在树状文本框,我想将我枚举:

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
    }

要图标。将有3个图标,让说ready.jpg,notready.jpg和AcceptedByAdmin.jpg

to Icons. There will be 3 icons, let say ready.jpg, notready.jpg and AcceptedByAdmin.jpg

国家及地区设有游泳池AcceptationStatusGlobalFlag和我都想要显示此枚举/图标

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>



推荐答案

创建值转换器

这需要你的枚举值,并返回相应的图标的文件名。

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

您将需要一个参考添加到该转换器你的XAML

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>



替换您的TextBlock

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}}"/>

这篇关于根据枚举在WPF树视图绑定图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆