0
我有一個層次集的數據綁定到菜單,它工作正常,但我顯示的菜單項圖標和名稱在一個stackpanel都在菜單的標題部分,因爲綁定的菜單項的圖標列沒有被使用,所以這會導致奇怪的外觀。想知道如何正確格式化我的XAML,以便各種綁定類型的圖標實際上綁定到MenuItem的Icon屬性,而不是坐在Header部分。在我的HierarchicalDataTemplate中使用樣式來正確地格式化我的MenuItem綁定
<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
爲我提供的古怪行爲,其中在層次結構中的最後一個菜單項包含根項目圖標:
一個更好更多的「破防」的方式這樣做可能是爲了實現一個IvalueConverter類型並檢查它,而不是盲目地檢查屬性是否存在。 – Wobbles