WPF - 绑定在简单的用户控制 [英] WPF - Binding in simple User Control

查看:148
本文介绍了WPF - 绑定在简单的用户控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题肯定是在我的脸上正确的,但我不能看到它... ...

My problem is certainly right on my face but I can't see it...

我建立一个非常简单的用户控制 - 三维椭圆 - 我揭露了两个属性:LightColor和DarkColor。我需要在我的用户控制这些属性渐变绑定,但它没有显示在所有任何颜色。这里是我的用户:

I am building a very simple user control - a 3D ellipse - and I expose two properties: LightColor and DarkColor. I need to bind these properties to the gradient in my user control, but it is not showing any color at all. Here's my usercontrol:

<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TestBrushes"
mc:Ignorable="d"
x:Class="TestBrushes._3DEllipse"
x:Name="UserControl"
d:DesignWidth="200" d:DesignHeight="200">
    <Grid x:Name="LayoutRoot">
        <Ellipse Name="MainEllipse" Stroke="{x:Null}">
            <Ellipse.Fill>
                <RadialGradientBrush GradientOrigin="0.5,1.1">
                    <GradientStop Color="{Binding ElementName=UserControl, Path=LightColor}" Offset="1"/>
                    <GradientStop Color="{Binding ElementName=UserControl, Path=DarkColor}"  Offset="0"/>
                </RadialGradientBrush>
            </Ellipse.Fill>
        </Ellipse>
        <Ellipse Name="TopReflectionEllipse" Stroke="{x:Null}" Margin="38,0,38,0" VerticalAlignment="Top" Height="90">
            <Ellipse.Fill>
                <RadialGradientBrush GradientOrigin="0.5,0">
                    <RadialGradientBrush.RelativeTransform>
                        <TransformGroup>
                            <ScaleTransform CenterX="0.5" CenterY="0.5" ScaleX="1" ScaleY="1"/>
                            <SkewTransform AngleX="0" AngleY="0" CenterX="0.5" CenterY="0.5"/>
                            <RotateTransform Angle="0" CenterX="0.5" CenterY="0.5"/>
                            <TranslateTransform X="0" Y="0"/>
                        </TransformGroup>
                    </RadialGradientBrush.RelativeTransform>
                    <GradientStop Color="#A5FFFFFF" Offset="0"/>
                    <GradientStop Color="#00FFFFFF" Offset="1"/>
                </RadialGradientBrush>
            </Ellipse.Fill>
        </Ellipse>
    </Grid>
</UserControl>

这是我的code-背后:

and here's my code-behind:

public partial class _3DEllipse
{
    public _3DEllipse()
    {
        InitializeComponent();
    }

    public Color DarkColor { get; set; }
    public Color LightColor { get; set; }
}

如果我指定的颜色,而不是在code所示约束力,但它的工作原理确定,但我想用我揭露的属性。我在做什么错了?

If I assign colors as opposed to the binding shown in the code, it works ok, but I want to use the properties I am exposing. What am I doing wrong?

谢谢!

推荐答案

您的问题很可能是你不notifiying到DarkColor和LightColor属性的值所做的更改的用户控件。要做到这一点,你就需要实现 INotifyPropertyChanged的接口。此接口的目的是使UI组件认识时,他们必然要被更新的价值;他们订阅由实现接口暴露PropertyChanged事件。

Your issue is likely that you aren't notifiying the UserControl of changes made to the value of the DarkColor and LightColor properties. To do so you'll need to implement the INotifyPropertyChanged interface. The purpose of this interface is to make UI components aware of when the value they're bound to is updated; they subscribe to the PropertyChanged event that is exposed by implementing the interface.

一个样本实现低于,我独自离开了DarkColor属性,但它会以同样的方式进行更新。

A sample implementation is below, I've left the DarkColor property alone, but it would be updated in the same fashion.

public partial class _3DEllipse : INotifyPropertyChanged
{

    private Color _lightColor;


    public _3DEllipse()
    {
        InitializeComponent();
    }

    // interface implementation
    public event PropertyChangedEventHandler PropertyChanged;

    public Color DarkColor { get; set; }
    public Color LightColor
    {
        get { return _lightColor; }
        set
        {
            // only update value if its changed
            if ( _lightColor == value )
            {
                return;
            }

            _lightColor = value;
            OnPropertyChanged("LightColor");
        }
    }

    protected virtual void OnPropertyChanged(string propertyName)
    {
        if ( PropertyChanged == null ) return;

        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

这篇关于WPF - 绑定在简单的用户控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆