Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
566 views
in Technique[技术] by (71.8m points)

c# - Bind Icon depending on Enum in WPF Treeview

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>

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...