从 WPF 中的 c# 代码设置静态样式资源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22753712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:23:55  来源:igfitidea点击:

Setting a static Style resource from c# Code in WPF

c#wpfxamlwpf-controls

提问by Asmaa Edress

My style testis defined as a resource in a ResourceDictionaryin UserControl, like this:

我的样式在 UserControl 中test定义为资源ResourceDictionary,如下所示:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<Style x:Key="test" TargetType="ContentControl">
    <!--<Setter Property="Visibility" Value="Visible" />-->
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ContentControl">
                <TextBlock Text="TESTTTT" Foreground="Black"/>
            </ControlTemplate>
          </Setter.Value>
       </Setter>
   </Style>
</ResourceDictionary>

UserControl:

用户控件:

<ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Dictionary1.xaml" />
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

In the code behind file of this UserControl I'am trying to get that Style and apply it to a content control, which is defined in the same UserControl.

在此 UserControl 的代码隐藏文件中,我试图获取该样式并将其应用于内容控件,该控件在同一个 UserControl 中定义。

Here is my UserControl.xaml:

这是我的 UserControl.xaml:

<UserControl x:Class="WpfApplication2.MainWindow"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             Height="350" Width="525" Loaded="Window_Loaded">
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Dictionary1.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>

    <Grid>
        <ContentControl Name="testControl" />
    </Grid>
</UserControl>

and in code-behind in the Loaded event handler I wrote:

在 Loaded 事件处理程序的代码隐藏中,我写道:

UserControl m = sender as UserControl;
Style s = m.Resources["test"] as Style; 
// I also tried to reference style in App resources, but it didn't make any difference 
//(Style)Application.Current.FindResource("test");
m.testControl = new ContentControl();
m.testControl.Style = s;
m.testControl.ApplyTemplate();

In debugging mode I saw the found style. Template Controls can also be found by searching for the using their keys/Names. But they wont be shown. It just show an empty user control without any controls from my template defined in the style.

在调试模式下,我看到了找到的样式。模板控件也可以通过搜索使用它们的键/名称来找到。但他们不会被显示。它只显示一个空的用户控件,没有在样式中定义的模板中的任何控件。

I hope you can help me and thanks in advance!

我希望你能帮助我,并提前致谢!

回答by Anatoliy Nikolaev

In this case, you create new ContentControl, but do not add to the current VisualTree, respectively, it is not visible.

在这种情况下,您创建 new ContentControl,但不添加到当前 VisualTree,分别是不可见的。

Also, there is no property testControlin a UserControl, because .symbol used for access to the properties of Class, therefore remove mbefore testControlor use thisinstead:

此外,不存在属性testControl中一个用户控件,因为.用于访问类的属性的符号,因此清除m之前testControl或使用this代替:

UserControl m = sender as UserControl;
Style s = m.Resources["test"] as Style; 
m.testControl = new ContentControl(); // Remove this line
m.testControl.Style = s;              // and 'm', or write like this: this.testControl.Style = s;
m.testControl.ApplyTemplate();

And the final result is:

最后的结果是:

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
    var control = sender as UserControl;

    if (control != null)
    {
        Style s = control.Resources["test"] as Style;
        testControl.Style = s;

        // control.ApplyTemplate(); // it's not necessary in your case
    }            
}