【问题标题】:Loading DataTemplate from a resource dictionary从资源字典加载 DataTemplate
【发布时间】:2012-09-03 05:17:54
【问题描述】:

在 WPF 客户端应用程序中,我在使用分层数据模板加载数据绑定菜单项时遇到了一些困难。

首先,我创建了 ViewModel,其中包含菜单项的基本属性:标题、命令、ImageSource(用于图标属性的图像的路径)和子项。

然后,我在 XAML 窗口中创建了视图以显示我的菜单。为了绑定我的集合,考虑到子项,我为菜单项模板使用了分层数据模板。

这是 ItemTemplate 的 XAML 代码:

<HierarchicalDataTemplate DataType="{x:Type vm:MenuItemViewModel}" ItemsSource="{Binding Path=Items}">
    <HierarchicalDataTemplate.ItemContainerStyle>
        <Style TargetType="MenuItem">
            <Style.Resources>
                <Image x:Key="ItemImage" Source="{Binding ImageSource}" Width="16" Height="16" x:Shared="false" />
            </Style.Resources>
            <Style.Setters>
                <Setter Property="Command" Value="{Binding Command}" />
                <Setter Property="CommandParameter" Value="{Binding CommandParameter}" />
                <Setter Property="Icon" Value="{StaticResource ItemImage}" />
            </Style.Setters>
        </Style>
    </HierarchicalDataTemplate.ItemContainerStyle>
    <StackPanel Orientation="Horizontal">
        <TextBlock Text="{Binding Title}" />
    </StackPanel>
</HierarchicalDataTemplate>

当我初始化菜单并显示窗口时,一切看起来都很好。

之后,我尝试将菜单项模板放入资源字典中,以便能够在我的应用程序中的任何位置将其作为默认模板重用。 当我这样做时,我会抛出一个异常:

命名空间中的共享属性 'http://schemas.microsoft.com/winfx/2006/xaml' 只能用于 编译资源字典。

花了这么多时间寻找解决方案,我终于做了一个测试项目(here)来演示这个问题。

我不知道如何使我的资源字典成为编译资源字典... 谁能帮帮我?

【问题讨论】:

    标签: wpf image data-binding menuitem hierarchicaldatatemplate


    【解决方案1】:

    尝试从&lt;Image&gt; 元素中删除x:Shared="false"

    【讨论】:

    • 如果属性被移除,只显示列表中的最后一张图片
    【解决方案2】:

    找到解决方案,实现转换器:

    public class MenuIconConverter : IValueConverter
    {
        #region IValueConverter Members
    
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null) return Binding.DoNothing;
    
            string imageUri = value.ToString();
    
            if (string.IsNullOrEmpty(imageUri)) return Binding.DoNothing;
    
            BitmapImage bitmapImage = new BitmapImage(new Uri(imageUri, UriKind.RelativeOrAbsolute)) { DecodePixelHeight = 16, DecodePixelWidth = 16 };
    
            return new Image() { Height = 16, Width = 16, Source = bitmapImage, SnapsToDevicePixels = true, UseLayoutRounding = true };
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return Binding.DoNothing;
        }
    
        #endregion
    }
    

    要使用转换器,需要修改 DataTemplate :

    <converter:MenuIconConverter x:Key="MenuIconConverter" />
    
    <HierarchicalDataTemplate x:Key="MenuItemTemplate" DataType="{x:Type vm:MenuItemViewModel}" ItemsSource="{Binding Path=Items}">
        <HierarchicalDataTemplate.ItemContainerStyle>
            <Style TargetType="MenuItem">
                <Style.Setters>
                    <Setter Property="Command" Value="{Binding Command}" />
                    <Setter Property="CommandParameter" Value="{Binding CommandParameter}" />
                    <Setter Property="Icon" Value="{Binding ImageSource, Converter={StaticResource MenuIconConverter}, Mode=OneWay}" />
                </Style.Setters>
            </Style>
        </HierarchicalDataTemplate.ItemContainerStyle>
        <TextBlock Text="{Binding Title}" />
    </HierarchicalDataTemplate>
    

    有了这个,一切正常。 我期待没有代码的解决方案,但似乎不可能:(

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-10
      • 2011-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多