将DynamicResource绑定到BasedOn Style

dea*_*ace 2 wpf xaml

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

  1. 我有一个风格(如MyTextStyle)在另一个文件中说" Generic.xaml "
  2. 我的文本框在ABC.xaml中
  3. 我想对这个Textbox应用一些触发器,所以我必须使用Textbox.Style
  4. 我也想申请" MyTextStyle "

当我做跟随它给我错误,我不能将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>
Run Code Online (Sandbox Code Playgroud)

请建议我一些解决方案,以便我可以应用此Dynamicresource以及我的数据触发器样式

Ale*_*der 8

更改DynamicResourceStaticResource这样的:

<Style BasedOn="{StaticResource MyTextStyle}" TargetType="{x:Type TextBox}">
Run Code Online (Sandbox Code Playgroud)

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

编辑: 你有"找不到名为'EmptyTextBoxStyle'的资源",因为应用程序找不到这个特定的静态资源.要帮助应用程序找到它,您需要使用MergedDictionary.以下是如何在Window中使用它的示例:

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/Generic.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>
Run Code Online (Sandbox Code Playgroud)

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

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="/Generic.xaml" />
</ResourceDictionary.MergedDictionaries>
Run Code Online (Sandbox Code Playgroud)

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