2

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

2
  • Why exactly do you want to use DynamicResource in BasedOn instead of StaticResource?
    – Alexander
    Sep 11, 2012 at 13:12
  • because style "MyTextStyle" is used for almost all textboxes in my solution. so it would be redundant if I define it specially(and locally) for this textbox purpose.
    – deathrace
    Sep 11, 2012 at 13:17

1 Answer 1

8

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

3
  • I tried this too. but it throws runtime exception. sorry, right now I am not able to show you the exception I am getting. will try this again and let you know tomorrow.
    – deathrace
    Sep 11, 2012 at 15:58
  • following exception occurred: "The composition produced a single composition error. The root cause is provided below. Review the CompositionException.Errors property for more detailed information. 1) Cannot find resource named 'EmptyTextBoxStyle'. Resource names are case sensitive". and Name is correct (i.e. 'EmptyTextBoxStyle' spelled correctly)
    – deathrace
    Sep 12, 2012 at 4:47
  • Hi. thanks. actually In my solution ResourceDictionary was added in one of the controller's constructor and that constructor gets called after initialization of this ABC.xaml. anyways it worked pefect. thanks very much
    – deathrace
    Sep 12, 2012 at 6:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.