wpf 将 DynamicResource 绑定到基于样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12370052/
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 05:18:17  来源:igfitidea点击:

binding DynamicResource to BasedOn Style

wpfxaml

提问by deathrace

My requirement is to apply multiple styles on a textbox having following situation:

我的要求是在具有以下情况的文本框上应用多种样式:

  1. I have one style(e.g. MyTextStyle) in another file say 'Generic.xaml'
  2. my textbox is in ABC.xaml
  3. I want to apply some triggers to this Textbox so I have to use Textbox.Style
  4. I also want to apply "MyTextStyle"
  1. 我有一个风格(如MyTextStyle在另一个文件中说)“ Generic.xaml
  2. 我的文本框在ABC.xaml
  3. 我想对这个 Textbox 应用一些触发器,所以我必须使用 Textbox.Style
  4. 我也想申请“ MyTextStyle

when I do following it gives me error that I cannot apply DynamicResource to BasedOn:

当我遵循它给我的错误,我不能将 DynamicResource 应用到 BasedOn:

<TextBox.Style>
                    <Style BasedOn="{DynamicResource MyTextStyle}" TargetType="{x:Type TextBox}">
                        <Setter Property="Text" Value="{Binding SelectedCall.Name}" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding SelectedCall.Name}" Value="N/A">
                                <Setter Property="Text" Value="" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </TextBox.Style>

please suggest me some solution so that I can apply this Dynamicresource as well as my datatrigger style

请给我建议一些解决方案,以便我可以应用此 Dynamicresource 以及我的 datatrigger 样式

回答by Alexander

Change DynamicResourceto StaticResourcelike this:

更改DynamicResourceStaticResource这样的:

<Style BasedOn="{StaticResource MyTextStyle}" TargetType="{x:Type TextBox}">

DynamicResource is intentionally not allowed in BasedOn.

在BasedOn 中故意不允许使用DynamicResource。

EDIT:You got "Cannot find resource named 'EmptyTextBoxStyle'" because application can't find this particular static resource. To help application to find it you need to use MergedDictionary. Here is the example of how to use it inside e.g. Window:

编辑:您收到“找不到名为 'EmptyTextBoxStyle' 的资源”,因为应用程序找不到这个特定的静态资源。为了帮助应用程序找到它,您需要使用 MergedDictionary。这是如何在例如 Window 中使用它的示例:

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/Generic.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

Inside another ResourceDictionary you should use this as the following:

在另一个 ResourceDictionary 中,您应该将其用作以下内容:

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

You need to reference ResourceDictionary that contains definition for EmptyTextBoxStyle style in this way. So for example, if 'EmptyTextBoxStyle' is declared in Generic.xaml file and you're using it in ABC.xaml you can just use the above XAML (of course, you need to update Source attribute according to your project structure).

您需要以这种方式引用包含 EmptyTextBoxStyle 样式定义的 ResourceDictionary。因此,例如,如果在 Generic.xaml 文件中声明了 'EmptyTextBoxStyle' 并且您在 ABC.xaml 中使用它,则可以只使用上面的 XAML(当然,您需要根据您的项目结构更新 Source 属性)。