Wpf 如何从CustomControl.cs C#文件访问XAML资源?

Wpf 如何从CustomControl.cs C#文件访问XAML资源?,wpf,resources,controls,Wpf,Resources,Controls,我有一个XXXCustomControl.cs类,在c#类中我想通过 groupStyle.ContainerStyle = this.FindResource("GroupHeaderStyle") as Style; GroupHeaderStyle,但此样式是在其他地方定义的(无论在何处…) 现在我的问题是:什么地方最适合我的团长风格,以及如何通过 FindResource from c#code?您应该将包含样式的XAML文件作为合并词典包含到应用程序的资源词典中: <Appli

我有一个XXXCustomControl.cs类,在c#类中我想通过

groupStyle.ContainerStyle = this.FindResource("GroupHeaderStyle") as Style;
GroupHeaderStyle,但此样式是在其他地方定义的(无论在何处…)

现在我的问题是:什么地方最适合我的团长风格,以及如何通过


FindResource from c#code?

您应该将包含样式的XAML文件作为合并词典包含到应用程序的资源词典中:

<Application.Resources>
    <ResourceDictionary>
        <!--  here you can add some more resources -->
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="mystyles.xaml"/>
            <!--  here you can add some dictionaries -->
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>
<Control.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/YourAssembly;component/Styles.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Control.Resources>

或者实际上,您可以直接将样式定义放入应用程序的资源中,而无需合并资源字典。但这样一来,应用程序的资源通常会很快膨胀

编辑:
对于库,您没有可用的App.xaml。因此,您需要基本上执行以下操作:

  • 将资源字典添加到项目中,并在其中定义所需的样式
  • 在控件的参考资料中,将词典称为合并词典
  • 请注意,您需要指定字典的完整路径(“”):

    <Application.Resources>
        <ResourceDictionary>
            <!--  here you can add some more resources -->
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="mystyles.xaml"/>
                <!--  here you can add some dictionaries -->
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
    
    <Control.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/YourAssembly;component/Styles.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Control.Resources>
    

    您应该将包含样式的XAML文件作为合并词典包含到应用程序的资源词典中:

    <Application.Resources>
        <ResourceDictionary>
            <!--  here you can add some more resources -->
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="mystyles.xaml"/>
                <!--  here you can add some dictionaries -->
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
    
    <Control.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/YourAssembly;component/Styles.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Control.Resources>
    

    或者实际上,您可以直接将样式定义放入应用程序的资源中,而无需合并资源字典。但这样一来,应用程序的资源通常会很快膨胀

    编辑:
    对于库,您没有可用的App.xaml。因此,您需要基本上执行以下操作:

  • 将资源字典添加到项目中,并在其中定义所需的样式
  • 在控件的参考资料中,将词典称为合并词典
  • 请注意,您需要指定字典的完整路径(“”):

    <Application.Resources>
        <ResourceDictionary>
            <!--  here you can add some more resources -->
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="mystyles.xaml"/>
                <!--  here you can add some dictionaries -->
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
    
    <Control.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/YourAssembly;component/Styles.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Control.Resources>
    

    如果您的样式是在ResourceDictionary中定义的,则始终可以使用“代码隐藏”访问它

    Uri resourceLocater = new Uri("/AssemblyName;component/DictionaryName.xaml", System.UriKind.Relative);
    ResourceDictionary resourceDictionary = (ResourceDictionary)Application.LoadComponent(resourceLocater);
    groupStyle.ContainerStyle = resourceDictionary["GroupHeaderStyle"] as Style; 
    

    如果您的样式是在ResourceDictionary中定义的,则始终可以使用在代码隐藏中访问它

    Uri resourceLocater = new Uri("/AssemblyName;component/DictionaryName.xaml", System.UriKind.Relative);
    ResourceDictionary resourceDictionary = (ResourceDictionary)Application.LoadComponent(resourceLocater);
    groupStyle.ContainerStyle = resourceDictionary["GroupHeaderStyle"] as Style; 
    

    是的,我是这样做的,我的程序项目,但我不认为控制项目需要/有一个app.xaml文件。似乎第三方免费web自定义控件项目是由作者ROFL:P撕毁的,似乎自定义控件库只有一个通用的.xaml。。。那么我应该如何从我的c#代码隐藏中访问这个generic.xaml呢?@Lisa:你是在开发一个库,而不是一个应用程序吗?实际上是的:-)10现在。。。LOL@Lisa:添加了库的示例。回答正确(+1)。我添加了另一种访问本地或引用程序集中定义的样式的方法。是的,我用我的programm项目使用了这种方法,但我认为控件项目不需要/有app.xaml文件。似乎第三方免费web自定义控件项目是由作者ROFL:P撕毁的,似乎自定义控件库只有一个通用的.xaml。。。那么我应该如何从我的c#代码隐藏中访问这个generic.xaml呢?@Lisa:你是在开发一个库,而不是一个应用程序吗?实际上是的:-)10现在。。。LOL@Lisa:添加了库的示例。回答正确(+1)。我添加了另一种访问本地或引用部件中定义的样式的方法。