Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.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/3/heroku/2.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
Wpf 将颜色绑定到UserControl中的DependencyProperty_Wpf_Colors - Fatal编程技术网

Wpf 将颜色绑定到UserControl中的DependencyProperty

Wpf 将颜色绑定到UserControl中的DependencyProperty,wpf,colors,Wpf,Colors,我有一个UserControl,它有一个Grid和一个绑定的Background属性。我的所有其他绑定都按预期工作,但出于某种原因,我在UserControl中得到的唯一颜色是我为dependencProperty设置的默认值 在main window.xaml中引用UserControl: <controls:MyUserControl Title="{Binding Path=MyObjects[0].Title" MyControlColor="{Binding Path=MyObj

我有一个
UserControl
,它有一个
Grid
和一个绑定的
Background
属性。我的所有其他绑定都按预期工作,但出于某种原因,我在
UserControl
中得到的唯一颜色是我为
dependencProperty
设置的默认值

main window.xaml
中引用
UserControl

<controls:MyUserControl Title="{Binding Path=MyObjects[0].Title" MyControlColor="{Binding Path=MyObjects[0].Color}" />
MyUserControl.xaml.cs
code:

public Color MyControlColor
{
    get { return (Color)GetValue(MyControlColorProperty); }
    set { SetValue(MyControlColorProperty, value); }
}
    public static readonly DependencyProperty MyControlColorProperty = DependencyProperty.Register("MyControlColor", typeof(Color), typeof(MyUserControl), new PropertyMetadata(Colors.Black));
然后是一个将颜色转换为
SolidColorBrush
的属性:

public SolidColorBrush MyControlColorBrush
{
    get { return new SolidColorBrush(MyControlColor); }
}

你知道我可能会错过什么吗?如果我检查
MyControlColor
的值,它会显示正确的颜色,但是
网格的背景不会从
黑色变为

页面首次加载时,绑定到MyControlColorBrush只会发生一次。您与MyObject[0].Color的绑定正在导致您的依赖项属性更新,但没有任何迹象表明应用程序的其余部分也需要更新MyControlColorBrush

有几种方法可以实现这一点,最简单的方法可能是为笔刷创建一个只读依赖属性,每当检测到颜色属性的更改时,就会更新该属性(这与宽度/实际宽度属性的工作方式类似)。您的控件需要颜色的DP:

public Color MyControlColor
{
    get { return (Color)GetValue(MyControlColorProperty); }
    set { SetValue(MyControlColorProperty, value); }
}

public static readonly DependencyProperty MyControlColorProperty =
    DependencyProperty.Register("MyControlColor", typeof(Color), typeof(MyUserControl),
        new PropertyMetadata(Colors.Black, OnColorChanged));
然后是刷子的只读DP:

public Brush MyControlColorBrush
{
    get { return (Brush)GetValue(MyControlColorBrushProperty); }
    protected set { SetValue(MyControlColorBrushPropertyKey, value); }
}

private static readonly DependencyPropertyKey MyControlColorBrushPropertyKey
        = DependencyProperty.RegisterReadOnly("MyControlColorBrush", typeof(Brush), typeof(MyUserControl),
                new FrameworkPropertyMetadata(Brushes.Black, FrameworkPropertyMetadataOptions.None));

public static readonly DependencyProperty MyControlColorBrushProperty = MyControlColorBrushPropertyKey.DependencyProperty;
当颜色DP发生变化时,您将更新画笔:

private static void OnColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    (d as MyUserControl).MyControlColorBrush = new SolidColorBrush((Color)e.NewValue);
}
自定义控件中的GUI元素然后绑定到只读DP,例如:

<Grid Background="{Binding Path=MyControlColorBrush, RelativeSource={RelativeSource AncestorType=local:MyUserControl}}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />

谢谢!我最终使用了其中的一部分,效果非常好。我去掉了我的常规ColorBrush属性,并用它创建了一个依赖属性,然后我创建了ColorPropertyChanged方法,我分配了如下颜色。。。(源代码为MyUserControl).MyControlColorBrush=新的SolidColorBrush((源代码为MyUserControl).MyControlColor);
<Grid Background="{Binding Path=MyControlColorBrush, RelativeSource={RelativeSource AncestorType=local:MyUserControl}}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />