0

Lets say I have a usercontrol that is basically a button, set up in XAML. Would it be possible for that button to get its background color from a different usercontrol class in a different file, so that it is changeable during runtime?

All I found from google is talk on themes and how they all want to refer to a theme file or the main window but I didn't find either of them useful because for one, I wouldn't have the slightest idea on how to change the theme file during runtime (if its even possible) and second, I plan to use these controls I'm buildings in other projects too so i don't want them to rely on anything to do with the main window.

Jake Freelander
  • 1,471
  • 1
  • 19
  • 26

2 Answers2

1

Use WPF's DynamicResource markup extension on the button's background colour, then you can change the resource, and thus the theme dynamically at runtime.

See the Stackoverflow answer to WPF Dynamically change resource file and theme.

Community
  • 1
  • 1
Phillip Trelford
  • 6,513
  • 25
  • 40
0

All of these explain things way too confusing while the answer is as simple as pie.

step1: add a resource directory file to your project and make your variables, something like

<SolidColorBrush x:Key="lsd" />

step2: add to each user control you want to access the resource

<UserControl.Resources>
    <ResourceDictionary Source="myResourceName.xaml"/>
</UserControl.Resources>

stel3: make something use the resource like

<Grid Background="{DynamicResource lsd}/>

step4: modify the variable in the resource in any of the affected controls by calling stuff like

Resources("lsd") = Brushes.Blue

step5: for additional hilarity add a sub like this to your control

Public Sub lsdMode() Handles Me.MouseMove
    Dim r As New Random
    Resources("sbL1") = New SolidColorBrush(Color.FromRgb(r.Next(255), r.Next(255), r.Next(255)))
End Sub
Jake Freelander
  • 1,471
  • 1
  • 19
  • 26