Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将控件属性绑定到绑定到其他属性的用户设置?_C#_Wpf_Xaml_Data Binding_Settings - Fatal编程技术网

C# 将控件属性绑定到绑定到其他属性的用户设置?

C# 将控件属性绑定到绑定到其他属性的用户设置?,c#,wpf,xaml,data-binding,settings,C#,Wpf,Xaml,Data Binding,Settings,最后,我希望跟进问题- 我想将此用户控件的颜色属性绑定到将传播到其他几个控件的用户设置属性 确切地说,我已经在代码XAML中这样做了,但是对用户控件所做的更改不会传播到应该接收它们的控件 在过去,我看到过使用一个简单的滑块来控制边框宽度的方法:但是,这不起作用。这里是讨论中的XAML <UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://s

最后,我希望跟进问题-

我想将此用户控件的颜色属性绑定到将传播到其他几个控件的用户设置属性

确切地说,我已经在代码XAML中这样做了,但是对用户控件所做的更改不会传播到应该接收它们的控件

在过去,我看到过使用一个简单的滑块来控制边框宽度的方法:但是,这不起作用。这里是讨论中的XAML

<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>
我有一个控件绑定来控制项目中设置的PlayerBGColor1和PlayerBGColor2的双向绑定,但是所做的更改不会立即反映出来,不管怎样,这些更改是针对应该从控件接收这些更改的用户控件的

这是用户设置中用于处理PlayerBG1和PlayerBG2颜色的控件绑定的XAML:

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

我在这里试图实现的是可能的,还是我需要找到另一种方法来实现这一点?

好的,我可能会对这个问题有一个答案-我认为问题是,当通过将Dependency属性绑定到我的用户设置时,我取消了已经存在的绑定,这样做Dependency属性就失去了它的来源,不再改变。为了防止出现这种情况,我必须在用户控件中写入一个回调方法,该方法将在颜色更改时引发一个事件,并直接更新settings属性。然后,读取该设置的任何内容都将被更新-

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

我已经对此进行了测试,它可以正常工作。

两个控件是否共享同一个DataContext实例?PlayerBGColor1和PlayerBGColor2属性是否通知更改,即它们是否引发PropertyChanged事件?@jhenninger24它们是-它们是继承ApplicationSettingsBase类的类的一部分。
public UCGradientTool( ) {
        InitializeComponent( );
        this.cdFirstColor.ColorChanged += ( S, E ) => Settings.Default.PlayerBGColor1 = E;
        this.cdSecondColor.ColorChanged += ( S, E ) => Settings.Default.PlayerBGColor2 = E;
    }