0

On my XAML i have defined a special style. Here i can access these with my defined resource like:

<TextBox Style="{StaticResource TransparentInput}" Text="Hello World!" [...] />

But how i can set the Style of an component programatically?

I wan't to add components programatically like

TextBox test = new TextBox();
test.Text = "Hello World!";
test.Style = [???] //<--- How i set here the static resource "TransparentInput"?
Adrian Preuss
  • 3,228
  • 1
  • 23
  • 43
  • Have a look here https://stackoverflow.com/questions/2117886/accessing-a-resource-via-codebehind-in-wpf – jag Jun 15 '18 at 15:01

1 Answers1

4

Try this:

test.Style = TryFindResource("TransparentInput") as Style;
mm8
  • 163,881
  • 10
  • 57
  • 88
  • Another "ugly" method: `test.Style = (Style) this.Resources["TransparentInput"];` – Adrian Preuss Jun 15 '18 at 15:06
  • The main benefit of using TryFindResource is that it simply returns null if the style cannot be found in the current scope. – mm8 Jun 15 '18 at 15:07