2010-05-07 30 views
15

Tôi có ít treeview TextBox, và tôi muốn chuyển đổi Enum của tôi:Biểu tượng Ràng buộc phụ thuộc vào Enum trong WPF TreeView

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

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

để Icons. Sẽ có 3 biểu tượng, chúng ta hãy nói ready.jpg, notready.jpg và AcceptedByAdmin.jpg

quốc gia và khu vực có hồ bơi AcceptationStatusGlobalFlag và trên cả hai tôi muốn hiển thị enum này/Biểu tượng

  <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

Đối với giải pháp không có bộ chuyển đổi, hãy xem: ht tp: //stackoverflow.com/questions/2787725/how-to-display-different-enum-icons-using-xaml-only/41150128#41150128 –

Trả lời

27

Tạo một Value Converter

Phải mất giá trị enum của bạn và trả về tên tập tin của các biểu tượng thích hợp.

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

Bạn sẽ cần phải thêm một tham chiếu đến chuyển đổi này trong XAML của bạn

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

Thay TextBlock bạn

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

với một hình ảnh và nói với nó sử dụng chuyển đổi của bạn

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

Bạn nên làm gì nếu đồ họa là hình dạng có thể mở rộng được xác định trong tài nguyên XAML , không phải là một bitmap trong một tập tin? – Anthony

+4

@Anthony bạn nên tạo một câu hỏi mới. – mak

Các vấn đề liên quan