0

Final (I hope) follow up to this question -

I want to bind the Color property of this User Control to a User Settings property that will disseminate to several other controls.

I've done that in code (XAML, to be precise), but changes made to the User Control are not propagating to the control that should be receiving them.

In the past I've seen this work (with a simple Slider controlling the Border Width) : However, this is not working. Here is the XAML in question.

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:S="clr-namespace:EQLControls.Properties"
    xmlns:Components="clr-namespace:WPFTools.Components;assembly=WPFTools"
    xmlns:Converters="clr-namespace:WPFTools.Classes.Converters;assembly=WPFTools"
    x:Class="EQLControls.Controls.UCPlayerPanel"
    mc:Ignorable="d"
    BorderThickness="{Binding PlayerBorderSize}"
    FontFamily="{Binding PlayerFontFamily}"
    FontWeight="{Binding PlayerFontWeight}"
    Height="50" Width="100">
    <UserControl.Background>
        <LinearGradientBrush EndPoint="1,1" StartPoint="0,0">
            <GradientStop Color="{Binding PlayerBGColor1}"/>
            <GradientStop Color="{Binding PlayerBGColor2}" Offset="1"/>
        </LinearGradientBrush>
    </UserControl.Background>
    <UserControl.BorderBrush>
        <SolidColorBrush Color="{Binding PlayerBorderColor}"/>
    </UserControl.BorderBrush>
    <UserControl.DataContext>
        <S:Settings/>
    </UserControl.DataContext>

I have a control bound to control the PlayerBGColor1 and PlayerBGColor2 of the Settings (Two way binding) in the project but changes made are not reflecting (instantly, anyway) against the User Control that is supposed to receive those changes from the control.

This is the XAML for the binding for the controls that are supposed to handle the PlayerBG1 and PlayerBG2 colors in the UserSettings :

<Controls:ColorDefiner Color="{Binding PlayerBGColor2, Mode=TwoWay}" Width="Auto" Height="Auto"/>
<Controls:ColorDefiner Color="{Binding PlayerBGColor1, Mode=TwoWay}" Width="Auto" Height="Auto"/>

Is what I am trying to achieve possible here, or will I need to find another way to do this?

Community
  • 1
  • 1
Will
  • 3,413
  • 7
  • 50
  • 107

1 Answers1

0

Okay I may have an answer to this question - The problem (I think) is that when by binding the Dependency Property to my User Setting, I knock off the binding that already exists, and in doing so the Dependency Property loses its source, and no longer changes. In order to prevent this, I have to write into the User Control a CallBack method which will raise an event when the color changes and update the settings property directly. Then, anything reading that setting will be updated -

public partial class ColorDefiner : UserControl {

    private event EventHandler<Color> ColorChangedEvent;

    public event EventHandler<Color> ColorChanged{
        add{this.ColorChangedEvent += value;}
        remove{this.ColorChangedEvent -= value;}
    }

    #region Dependency Properties
    public static readonly DependencyProperty
        ColorProperty = DependencyProperty.Register( "Color", typeof( Color ), typeof( ColorDefiner ), new FrameworkPropertyMetadata(
            Colors.Black, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, ColorDefiner.OnColorPropertyChanged ) );

    private static void OnColorPropertyChanged( DependencyObject d, DependencyPropertyChangedEventArgs e ) {
        ColorDefiner CD = d as ColorDefiner;
        if ( CD.ColorChangedEvent != null )
            CD.ColorChangedEvent( CD, (Color)e.NewValue );
    }
    #endregion

And then it's as simple as tying onto the event handler -

public UCGradientTool( ) {
        InitializeComponent( );
        this.cdFirstColor.ColorChanged += ( S, E ) => Settings.Default.PlayerBGColor1 = E;
        this.cdSecondColor.ColorChanged += ( S, E ) => Settings.Default.PlayerBGColor2 = E;
    }

I've tested this and it works.

Will
  • 3,413
  • 7
  • 50
  • 107