这是these two个问题的后续跟进。 我有一个自定义用户控件,旨在允许用户调整颜色A,R,G和B通道:
这些滑块中的每一个都通过Color.FromScRgb
方法控制颜色的一个方面。
每个滑块从0到1运行。
我已经实现了一个类来处理来自控件的输入 - 一个Color View Model类:
public class ColorViewModel : INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged;
private Color _Color = Colors.Black;
public double A {
get { return this.Color.ScA; }
set {
this._Color.ScA = ( float )value;
if ( this.PropertyChanged != null ) {
this.PropertyChanged( this, new PropertyChangedEventArgs( "A" ) );
this.PropertyChanged( this, new PropertyChangedEventArgs( "Color" ) );
}
}
}
public double R {
get { return this.Color.ScR; }
set {
this._Color.ScR = ( float )value;
if ( this.PropertyChanged != null ) {
this.PropertyChanged( this, new PropertyChangedEventArgs( "R" ) );
this.PropertyChanged( this, new PropertyChangedEventArgs( "Red" ) );
this.PropertyChanged( this, new PropertyChangedEventArgs( "Color" ) );
}
}
}
public double G {
get { return this.Color.ScG; }
set {
this._Color.ScG = ( float )value;
if ( this.PropertyChanged != null ) {
this.PropertyChanged( this, new PropertyChangedEventArgs( "G" ) );
this.PropertyChanged( this, new PropertyChangedEventArgs( "Green" ) );
this.PropertyChanged( this, new PropertyChangedEventArgs( "Color" ) );
}
}
}
public double B {
get { return this._Color.ScB; }
set {
this._Color.ScB = ( float )value;
if ( this.PropertyChanged != null ) {
this.PropertyChanged( this, new PropertyChangedEventArgs( "B" ) );
this.PropertyChanged( this, new PropertyChangedEventArgs( "Blue" ) );
this.PropertyChanged( this, new PropertyChangedEventArgs( "Color" ) );
}
}
}
public Color Color {
get { return this._Color; }
set {
this._Color = value;
if ( this.PropertyChanged != null )
this.AllChanged( );
}
}
public Color Red { get { return Color.FromScRgb( 1.0F, ( float )this.R, 0.0F, 0.0F ); } }
public Color Green { get { return Color.FromScRgb( 1.0F, 0.0F, ( float )this.G, 0.0F ); } }
public Color Blue { get { return Color.FromScRgb( 1.0F, 0.0F, 0.0F, ( float )this.B ); } }
private void AllChanged( ) {
this.PropertyChanged( this, new PropertyChangedEventArgs( "A" ) );
this.PropertyChanged( this, new PropertyChangedEventArgs( "R" ) );
this.PropertyChanged( this, new PropertyChangedEventArgs( "G" ) );
this.PropertyChanged( this, new PropertyChangedEventArgs( "B" ) );
this.PropertyChanged( this, new PropertyChangedEventArgs( "Red" ) );
this.PropertyChanged( this, new PropertyChangedEventArgs( "Green" ) );
this.PropertyChanged( this, new PropertyChangedEventArgs( "Blue" ) );
this.PropertyChanged( this, new PropertyChangedEventArgs( "Color" ) );
}
}
通讯效果很好 - 每当我调整控制设置时,我都能看到它反映在CVM课程中。这很好,但它对我没有帮助,因为我需要一种从CVM类访问颜色的方法。我使用CVM类的一个实例作为控件的数据上下文,然后我尝试将Control中的自定义属性绑定到控件数据上下文,但它似乎没有工作:
以下是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:Components="clr-namespace:WPFTools.Components"
xmlns:Controls="clr-namespace:WPFTools.Controls"
x:Class="WPFTools.Controls.ColorDefiner"
mc:Ignorable="d"
Width="120" Height="109.25" FontFamily="Arial" FontWeight="Bold">
<UserControl.DataContext>
<Controls:ColorViewModel/>
</UserControl.DataContext>
这是控制类代码(如果有人认为有必要,我可以提供XAML,但是现在我会把它留下来 - 因为它非常详细) -
public partial class ColorDefiner : UserControl {
public static readonly DependencyProperty
_Color = DependencyProperty.Register( "Color", typeof( Color ), typeof( ColorDefiner ) );
public Color Color {
get { return ( Color )this.GetValue( ColorDefiner._Color ); }
set { this.SetValue( ColorDefiner._Color, value ); }
}
private ColorViewModel CVM { get { return this.DataContext as ColorViewModel; } }
public ColorDefiner( ) {
InitializeComponent( );
//There is no error here, but it seems that the color is not being set in the Color property of this control!
Binding B = new Binding( "Color" ) { Source = this.DataContext, Mode = BindingMode.TwoWay };
this.SetBinding( ColorDefiner._Color, B );
}
}
我在Set Color属性上设置了一个断点,但是当我调整其中一个滑块时它没有被击中。
我在这里正确实施MVVM吗? 为什么CVM没有更新ColorDefiner颜色属性?
有人建议我可以绑定到DataContextChanged事件 - 这不起作用。
public ColorDefiner( ) {
this.DataContextChanged += ( S, E ) => {
Binding B = new Binding( "Color" ) { Source = this.DataContext, Mode = BindingMode.TwoWay };
this.SetBinding( ColorDefiner._Color, B );
};
InitializeComponent( );
}
我尝试使用InitializeComponent下面的事件,但调用顺序错误,事件从未触发。以这种方式,事件被触发,但CVM仍未设置依赖项属性。也;我不认为这是问题,就好像它是,然后DataContext将为null并且应该抛出异常(我认为)。