Set Binding over App.xaml programmatically

99 Views Asked by At

I want to use a VisualBrush in App.xaml:

<Application.Resources>
    <VisualBrush x:Key="TestBrush1" />
    <VisualBrush x:Key="TestBrush2" />
</Application.Resources>

Then I want to set a binding in one window like this:

VisualBrush testBrush = (VisualBrush)FindResource("TestBrush1");
Binding testBinding = new Binding();
testBinding.Source = FirstBrowser;
testBrush.Visual = testBinding;

And then I want to use this Brush to show the content of the FirstBrowser to another window. But that would come later. The binding does not work this way. Has someone an idea how I can make this work?

1

There are 1 best solutions below

1
mm8 On

You cannot modify a VisualBrush that you have defined in App.xaml because it is frozen automatically by the runtime so defining a VisualBrush without a Visual as a global resource is useless.

You better create a new brush when you actually need it. You can bind the Visual property of a VisualBrush using the BindingOperations.SetBinding method like this:

VisualBrush testBrush = new VisualBrush();
BindingOperations.SetBinding(testBrush, VisualBrush.VisualProperty, new Binding() { Source = FirstBrowser });