deathrace
deathrace

Reputation: 1073

binding DynamicResource to BasedOn Style

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"

when I do following it gives me error that I cannot apply DynamicResource to 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

Upvotes: 2

Views: 7094

Answers (1)

Alexander
Alexander

Reputation: 1319

Change DynamicResource to StaticResource like this:

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

DynamicResource is intentionally not allowed in BasedOn.

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:

<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.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).

Upvotes: 8

Related Questions