22

How can you set the following in code behind?

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

I'm using a Theme merged in App.xaml. It works great for all Controls but when I define a Style for something, e.g. TextBox, the Theme Style doesn't get picked up unless I use BasedOn like above, instead it gets the default TextBox Style.

Now I'm creating a DataGridTextColumn in code behind and I can't get the BasedOn part to work for the EditingElementStyle

Style editingStyle = new Style(typeof(TextBox));
editingStyle.BasedOn = ...?;

Any suggestions? Also, is there any way to get the Theme Style instead of the default Style applied without using BasedOn?

Thanks

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Fredrik Hedblad
  • 83,499
  • 23
  • 264
  • 266
  • You are trying to get the Theme applied to a given control and have the local style override the Theme components which it affects? – Aaron McIver Mar 04 '11 at 15:07
  • 1
    @Aaron: I'm using a Theme which affects all controls. But in many places I need to add specific setters, triggers etc. Binding `ListBoxItem.IsSelected` in the `ListBox` ItemContainerStyle for example. I want the Theme Style applied with an additional setter – Fredrik Hedblad Mar 04 '11 at 15:12

4 Answers4

26

Try this:

editingStyle.BasedOn = (Style) FindResource(typeof (TextBox))

And I don't know any way how you can make it apply the Theme style without specifying BasedOn. If there is such a way, I would like to know it too...

codekaizen
  • 26,990
  • 7
  • 84
  • 140
Pavlo Glazkov
  • 20,498
  • 3
  • 58
  • 71
6

This should work:

Style baseStyle = new Style(typeof(TextBox));
Style editingStyle = new Style(typeof(TextBox));
editingStyle.BasedOn = baseStyle;

You can also do it in the constructor:

Style editingStyle = new Style(typeof(TextBox), baseStyle);
Edwin de Koning
  • 14,209
  • 7
  • 56
  • 74
  • Thanks for your answer! Yes, I hoped that would work to but the Theme Style isn't picked up for some reason. If I do it in Xaml it works.. – Fredrik Hedblad Mar 04 '11 at 14:58
2

I like the answer of Pavlo Glazkov, but it does not compile.

FindResource is (non-static) member of FrameworkElement. It is required to identify the context of the search request.

So I recommend this:

style.BasedOn = (Style)frameworkElement.FindResource(typeof(TextBox));
Community
  • 1
  • 1
lg2de
  • 616
  • 4
  • 16
0

Here's a post that occurred a few years after this question and provides a functional way to reference theme from XAML. Per https://stackoverflow.com/a/24286059/5104896

First - Define the XAML resource dictionary with an x:class identifier along with an x:Key of your resource

ResourceDictionary x:Class="YourNameSpaceHere.DataGridCellTemplates"

<Style x:Key="ValidationError_TextBox" TargetType="{x:Type TextBox}">
    <Setter Property="Validation.ErrorTemplate" Value="{StaticResource ValidationErrorTempate}" />
</Style>

Second - Create a .cs file that will initialize the resource class.

using System.Windows;

namespace YourNameSpaceHere
{
    public partial class DataGridCellTemplates : ResourceDictionary
    {
        public DataGridCellTemplates()
        {
            InitializeComponent();
        }
    }
}

Lastly - Reference from you code

using YourNameSpaceHere;
....
var res = new DataGridCellTemplates();
Style test = res["ValidationError_TextBox"] as Style;
Galactic
  • 400
  • 4
  • 14