0

I have created sliders in settings.xaml.cs and added those to a solid brush color. I cannot access them from anywhere except code behind. Is there a way to call them from xaml?

    public void sli_ValueChanged(object sender, System.Windows.RoutedPropertyChangedEventArgs<double> e)
    {
        SolidColorBrush FontBrush = (SolidColorBrush)this.Resources["FontBrush"];
        //SolidColorBrush FontBrush = (SolidColorBrush)TryFindResource("FontBrush");

        if ((sliR != null) && (sliG != null) && (sliB != null))
        {
            FontBrush.Color = Color.FromRgb((byte)sliR.Value, (byte)sliG.Value, (byte)sliB.Value);
        }

        settings.FontBrush = FontBrush;
    }
}

This is where the new brush is created and it can be called in code behind but not in any XAML except settings.xaml.

Community
  • 1
  • 1
  • Can no one answer this question? I am really needing help. I have to turn this project in in 2 days and I am frankly at a complete loss of how to make this work. – Krystal Isabel Shaw May 06 '17 at 23:39
  • I do not really understand what are you trying to do.. Binding "FontBrush" from the settings.xaml.cs Resources? How to you use settings.xaml? Binding like {Binding FontBrush} does not work? – Dávid Molnár May 07 '17 at 16:44
  • @David Molnar, Thank you for the reply. Basically what I am trying to do is. I have 3 sliders in my settings.xaml page. In the cs for settings.xaml it specifies what happens when the slider changes. and creates a new solidcolorbrush from the value of the sliders. Now my problems is that I need to be able to access this solidcolorbrush from other windows. And because it's created in settings.xaml and not app I can't seem to access it. Any suggestions? – Krystal Isabel Shaw May 07 '17 at 20:07
  • Possible duplicate of [Bind to a value defined in the Settings](http://stackoverflow.com/questions/845030/bind-to-a-value-defined-in-the-settings) – Kamil Solecki May 08 '17 at 07:29

1 Answers1

0

Create the SolidColorBrush resource in the App.xaml:

<Application.Resources>
     <SolidColorBrush x:Key="MyFontBrush" Color="Red"></SolidColorBrush>
</Application.Resources>

The resource will be available to the whole application. You will be able to bind to it from anywhere in the application.

Then you use binding like this:

<TextBlock FontBrush="{DynamicResource MyFontBrush}" />

Use dynamic binding, because you want to change the value dynamically.

In the settings.xaml.cs use this:

if ((sliR != null) && (sliG != null) && (sliB != null))
{
    var newColor = Color.FromRgb((byte)sliR.Value, (byte)sliG.Value, (byte)sliB.Value);
    Application.Current.Resources["MyFontBrush"] = new SolidColorBrush(newColor);

    // changing the color value directly does not work
    // for me throws exception saying the object is in read only state
    // ... MyFontBrush.Color = newColor
}
Dávid Molnár
  • 10,673
  • 7
  • 30
  • 55