我声明了两个Dependency属性: 首先,类型为Color的FilterColor 和第二个类型为BrushBrush的Brush。 当FilterBrush.Color属性发生变化时,我需要更新FilterColor的值,当FilterColor属性发生变化时,我需要更新FilterBrush.Color的值。 我怎么能意识到它?
答案 0 :(得分:0)
使用TwoWay绑定绑定两个属性,如果在UI中更改一个属性,请在属性设置器和反向Versa中更改另一个属性,并使用INotifyPropertyChanged通知UI该属性已更改。
答案 1 :(得分:0)
您可以在DependencyProperty定义中执行此操作,也可以使用DependencyPropertyDescriptor之后执行此操作
例如......
DependencyProperty定义:
public static readonly DependencyProperty FilterColorProperty =
DependencyProperty.Register("FilterColor", typeof(Color),
typeof(MyUserControl),
new PropertyMetadata(Colors.White, FilterColorChanged));
public static void FilterColorChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
if (!(obj is MyUserControl))
return;
MyUserControl ctrl = (MyUserControl)obj;
var brush = ctrl.GetBrushProperty();
if (brush.Color != (Color)e.NewValue)
brush.Color = (Color)e.NewValue;
}
DependencyProperty Descriptor:
DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(
MyUserControl.FilterColorProperty, typeof(MyUserControl));
if (dpd != null)
dpd.AddValueChanged(this, delegate { FilterColor_Changed(); });
...
private void FilterColor_Changed()
{
Color filterColor = GetFilterColor(this);
var brush = GetBrush(this);
if (filterColor != brush.Color)
brush.Color = filterColor;
}
我可能有一些语法错误......我没有编译器来检查代码
答案 2 :(得分:0)
您在哪里定义了这些属性:在视图模型中还是在控件中?
如果它在视图模型中,你应该使用INotifyPropertyChanged而不是像这样的依赖属性:
Color _filterColor;
public Color FilterColor
{
get
{
return _filterColor;
}
{
if (_filterColor != value)
{
_filterColor = value;
RaisePropertyChanged(() => FilterColor);
_OnFilterColorChanged();
}
}
void _OnFilterColorChanged()
{
_filterBrush= ...
RaisePropertyChanged(() => FilterBrush);
}
Brush _filterBrush;
public Brush FilterBrush
{
get
{
return _filterBrush;
}
{
if (_filterBrush != value)
{
_filterBrush = value;
RaisePropertyChanged(() => FilterBrush);
_OnFilterBrushChanged();
}
}
void _OnFilterBrushChanged()
{
_filterColor= ...
RaisePropertyChanged(() =. FilterColor);
}
如果它在控制中,请执行以下操作:
public Color FilterColor
{
get { return (Color)GetValue(FilterColorProperty); }
set { SetValue(FilterColorProperty, value); }
}
public static readonly DependencyProperty FilterColorProperty =
DependencyProperty.Register("FilterColor", typeof(Color), typeof(MainWindow), new UIPropertyMetadata(Colors.Transparent, new PropertyChangedCallback(_OnFilterColorPropertyChanged)));
static void _OnFilterColorPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var mw = d as MainWindow;
Color oldValue = (Color)e.OldValue;
Color newValue = (Color)e.NewValue;
if (null != mw && oldValue != newValue)
{
mw._OnFilterColorChanged(oldValue, newValue);
}
}
bool _isFilterColorUpdating = false;
void _OnFilterColorChanged(Color oldValue, Color newValue)
{
if (_isFilterBrushUpdating )
return;
_isFilterColorUpdating = true;
Brush = ...
_isFilterColorUpdating = false;
}
public Brush FilterBrush
{
get { return (Brush)GetValue(FilterBrushProperty); }
set { SetValue(FilterBrushProperty, value); }
}
public static readonly DependencyProperty FilterBrushProperty =
DependencyProperty.Register("FilterBrush", typeof(Brush), typeof(MainWindow), new UIPropertyMetadata(Brushs.Transparent, new PropertyChangedCallback(_OnFilterBrushPropertyChanged)));
static void _OnFilterBrushPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var mw = d as MainWindow;
Brush oldValue = (Brush)e.OldValue;
Brush newValue = (Brush)e.NewValue;
if (null != mw && oldValue != newValue)
{
mw._OnFilterBrushChanged(oldValue, newValue);
}
}
bool _isFilterBrushUpdating = false;
void _OnFilterBrushChanged(Brush oldValue, Brush newValue)
{
if (_isFilterColorUpdating )
return;
_isFilterBrushUpdating = true;
Color = ...
_isFilterBrushUpdating = false;
}
请注意,最后一种方式只是破解,这是非常糟糕的方式,我更喜欢第一种方式。