2016-02-28 41 views
0

我有一個層次集的數據綁定到菜單,它工作正常,但我顯示的菜單項圖標和名稱在一個stackpanel都在菜單的標題部分,因爲綁定的菜單項的圖標列沒有被使用,所以這會導致奇怪的外觀。想知道如何正確格式化我的XAML,以便各種綁定類型的圖標實際上綁定到MenuItem的Icon屬性,而不是坐在Header部分。在我的HierarchicalDataTemplate中使用樣式來正確地格式化我的MenuItem綁定

enter image description here

<Menu x:Name="menu" > 
     <Menu.Resources> 
      <HierarchicalDataTemplate DataType="{x:Type ODIF:PluginContainer}"> 
       <HierarchicalDataTemplate.ItemsSource> 
        <MultiBinding> 
         <MultiBinding.Converter> 
          <local:CompositeDeviceCollectionConverter /> 
         </MultiBinding.Converter> 
         <Binding/> 
         <Binding Path="Instance.Devices"/> 
        </MultiBinding> 
       </HierarchicalDataTemplate.ItemsSource> 
       <StackPanel HorizontalAlignment="Left" Orientation="Horizontal"> 
        <Image Source="{Binding PluginIcon}" Width="16" Height="16"> 
         <Image.Style> 
          <Style TargetType="{x:Type Image}"> 
           <Style.Triggers> 
            <DataTrigger Binding="{Binding PluginIcon}" Value="{x:Null}"> 
             <Setter Property="Visibility" Value="Collapsed" /> 
            </DataTrigger> 
           </Style.Triggers> 
          </Style> 
         </Image.Style> 
        </Image> 
        <TextBlock Text="{Binding PluginName}"/> 
       </StackPanel> 
      </HierarchicalDataTemplate> 

      <HierarchicalDataTemplate DataType="{x:Type ODIF:OutputDevice}" ItemsSource="{Binding InputChannels}"> 
       <StackPanel HorizontalAlignment="Left" Orientation="Horizontal"> 
        <Image Source="{Binding StatusIcon}" Width="16" Height="16"> 
         <Image.Style> 
          <Style TargetType="{x:Type Image}"> 
           <Style.Triggers> 
            <DataTrigger Binding="{Binding StatusIcon}" Value="{x:Null}"> 
             <Setter Property="Visibility" Value="Collapsed" /> 
            </DataTrigger> 
           </Style.Triggers> 
          </Style> 
         </Image.Style> 
        </Image> 
        <TextBlock Text="{Binding DeviceName}"/> 
       </StackPanel> 
      </HierarchicalDataTemplate> 
      <HierarchicalDataTemplate DataType="{x:Type ODIF:InputDevice}" ItemsSource="{Binding OutputChannels}"> 
       <StackPanel HorizontalAlignment="Left" Orientation="Horizontal"> 
        <Image Source="{Binding StatusIcon}" Width="16" Height="16"> 
         <Image.Style> 
          <Style TargetType="{x:Type Image}"> 
           <Style.Triggers> 
            <DataTrigger Binding="{Binding StatusIcon}" Value="{x:Null}"> 
             <Setter Property="Visibility" Value="Collapsed" /> 
            </DataTrigger> 
           </Style.Triggers> 
          </Style> 
         </Image.Style> 
        </Image> 
        <TextBlock Text="{Binding DeviceName}"/> 
       </StackPanel> 
      </HierarchicalDataTemplate> 
      <HierarchicalDataTemplate DataType="{x:Type ODIF:DeviceChannel}"> 
       <local:ChannelBox Channel="{Binding}" Width="200" Click="ChannelClicked"/> 
      </HierarchicalDataTemplate> 
     </Menu.Resources> 
     <MenuItem x:Name="menuItem" BorderThickness="0" 
        Width="{Binding ActualWidth, ElementName=menu, Mode=OneWay}" 
        Height="{Binding ActualHeight, ElementName=menu, Mode=OneWay}" 
        ItemsSource="{Binding Plugins, ElementName=InputChannelBoxMenuControl}" Padding="0"> 
      <MenuItem.Header> 
       <local:ChannelBox x:Name="SelectedChannelBox" Width="{Binding ActualWidth, ElementName=menuItem, Mode=OneWay}" Height="{Binding ActualHeight, ElementName=menuItem, Mode=OneWay}" Channel="{Binding SelectedChannel, ElementName=InputChannelBoxMenuControl}"/> 
      </MenuItem.Header> 
     </MenuItem> 

    </Menu> 

UPDATE:

編輯HierarchicalDataTemplate.ItemContainerStyle爲我提供的古怪行爲,其中在層次結構中的最後一個菜單項包含根項目圖標: enter image description here

回答

0

我找到了根據另一個用戶的答案來實現此目的,這個答案已經被刪除,只需要多一點修改就可以處理我在b中使用的不同類型inding。

這感覺很「駭人」,所以如果有人想到更好的東西,我希望他們會發布,但在那之前我已經瞭解到,有時你必須欺騙和或強壯的WPF做你想做的事情,並希望它不'不會破壞道路;

添加這對我的根菜單項所取得的成果,我在尋找:

  <MenuItem.ItemContainerStyle> 
       <Style TargetType="{x:Type MenuItem}"> 
        <Style.Resources> 
         <local:IsNullConverter x:Key="isNullConverter"/> 
         <Image x:Key="IconImage" x:Shared="False"> 
          <Image.Style> 
           <Style TargetType="{x:Type Image}"> 
            <Style.Triggers> 
             <DataTrigger Binding="{Binding PluginIcon, Converter={StaticResource isNullConverter}}" Value="False"> 
              <Setter Property="Source" Value="{Binding PluginIcon}" /> 
             </DataTrigger> 
             <DataTrigger Binding="{Binding StatusIcon, Converter={StaticResource isNullConverter}}" Value="False"> 
              <Setter Property="Source" Value="{Binding StatusIcon}" /> 
             </DataTrigger> 
            </Style.Triggers> 
           </Style> 
          </Image.Style> 
         </Image> 
        </Style.Resources> 
        <Setter Property="Icon" Value="{StaticResource IconImage}" /> 
       </Style> 
      </MenuItem.ItemContainerStyle> 

和轉換器:

public class IsNullConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     return (value == null); 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     throw new InvalidOperationException("IsNullConverter can only be used OneWay."); 
    } 
} 

enter image description here

+0

一個更好更多的「破防」的方式這樣做可能是爲了實現一個IvalueConverter類型並檢查它,而不是盲目地檢查屬性是否存在。 – Wobbles

相關問題