2010-05-07 8 views
15

mam w treeview TextBox i chcę przekonwertować ENUM:Bind Ikona zależności od Enum w WPF Treeview

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

public enum AcceptationStatusGlobalFlag 
    { 
     NotReady = 0, 
     Ready = 1, 
     AcceptedByAdmin=2 
    } 

na ikony. Nie będzie 3 ikony, powiedzmy ready.jpg, notready.jpg i AcceptedByAdmin.jpg

kraju i regionu posiada basen AcceptationStatusGlobalFlag i zarówno ja chcesz wyświetlić ENUM/ikony

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

+0

Rozwiązanie bez konwertera patrz: ht tp: //stackoverflow.com/questions/2787725/how-to-display-different-enum-icons-using-xaml-only/41150128#41150128 –

Odpowiedz

27

Tworzenie Value Converter

to ma swoją wartość enum i zwraca nazwę pliku odpowiednią ikonę.

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

Musisz dodać odwołanie do tego konwertera w XAML

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

zastąpić TextBlock

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

z obrazem i powiedzieć, że korzystanie z konwertera

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

Co powinieneś zrobić, jeśli grafika jest skalowalnym kształtem zdefiniowanym w zasobie XAML , a nie mapa bitowa w pliku? – Anthony

+4

@Anthony powinieneś utworzyć nowe pytanie. – mak

Powiązane problemy