如何向具有ResourceDictionary的类添加资源

时间:2015-04-16 06:44:03

标签: c# wpf wpf-controls

我在代码中添加<s:RsuSensorVisible x:Key="RsuSensorLocalVisible"/>时出现问题( x:需要关键属性)。代码张贴在下面,附上图片。他们似乎互相取消了。如果我删除<s:RsuSensorVisible x:Key="RsuSensorLocalVisible"/>完美无缺。一些解决方案?

public class RsuSensorVisible : IValueConverter
                {
                    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
                    {
                        //string stringValue = ((ContentControl)value).Content;
                        var stringValue = value as string;

                        if (!string.IsNullOrEmpty(stringValue))
                        {
                            return stringValue == "RSU" ? Visibility.Visible : Visibility.Hidden;
                        }
                        return Visibility.Visible;
                    }

                    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
                    {
                        throw new NotImplementedException();
                    }
                }

    <Window.Resources>
           <s:RsuSensorVisible x:Key="RsuSensorLocalVisible"/> 
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="Resources/DesignerItem.xaml" />
                    <ResourceDictionary Source="Resources/ToolboxItem.xaml" />
                    <ResourceDictionary Source="Resources/Toolbox.xaml" />
                    <ResourceDictionary Source="Resources/Connection.xaml" />
                    <ResourceDictionary Source="Resources/Stencils/FlowChartStencils.xaml" />
                    <ResourceDictionary Source="Resources/Stencils/ShapeStencils.xaml" />
                    <ResourceDictionary Source="Resources/Stencils/SymbolStencils.xaml" />
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Window.Resources>

enter image description here

2 个答案:

答案 0 :(得分:1)

只需将RsuSensorVisible变量放在ResourceDictionary声明中,问题就会消失。 Window.Resources元素的内容必须是ResourceDictionary。

    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources/DesignerItem.xaml" />
                <ResourceDictionary Source="Resources/ToolboxItem.xaml" />
                <ResourceDictionary Source="Resources/Toolbox.xaml" />
                <ResourceDictionary Source="Resources/Connection.xaml" />
                <ResourceDictionary Source="Resources/Stencils/FlowChartStencils.xaml" />
                <ResourceDictionary Source="Resources/Stencils/ShapeStencils.xaml" />
                <ResourceDictionary Source="Resources/Stencils/SymbolStencils.xaml" />
            </ResourceDictionary.MergedDictionaries>

            <s:RsuSensorVisible x:Key="RsuSensorLocalVisible"/> 
        </ResourceDictionary>
    </Window.Resources>

答案 1 :(得分:1)

我无法评论,所以我会发一个答案。

您是否尝试将<s:RsuSensorVisible x:Key="RsuSensorLocalVisible"/>放入ResourceDictionary中?或者,尝试将x:Key属性赋予ResourceDictionary。看起来这就是问题