Reputation: 1886
As I know button does not create new instance, it uses shared resource. Why is button's background blue and not Red?
XAML:
<StackPanel Name="st">
<Button Margin="50" Name="btn" Height="50">Click</Button>
</StackPanel>
Code behind:
st.Resources.Add("Back", Brushes.Blue);
btn.Background = (Brush)btn.TryFindResource("Back");
st.Resources["Back"] = Brushes.Red;
Upvotes: 0
Views: 39
Reputation: 63317
Once setting the Background
, it's just kind of snapshotting. Changing the Resource after that won't change the Background
. You have to use some kind of setting resource reference using SetResourceReference
method like this:
st.Resources.Add("Back", Brushes.Blue);
btn.SetResourceReference(Control.BackgroundProperty, "Back");
st.Resources["Back"] = Brushes.Red;
Upvotes: 1