Hom*_*mam 3 c# wpf dependency-properties
如何创建自定义只读布尔依赖项属性,该属性返回And两个自定义布尔依赖项属性之间的操作,例如(A,B),
当A或B发生变化时,我希望触发result属性.
任何帮助实现这一目标!
第1部分:依赖关系.
public static readonly DependencyProperty Source1Property =
DependencyProperty.Register(
"Source1",
typeof(bool),
typeof(MyControl),
new FrameworkPropertyMetadata(false, new PropertyChangedCallback(UpdateTarget)));
public bool Source1
{
get { return (bool)GetValue(Source1Property); }
set { SetValue(Source1Property, value); }
}
void UpdateTarget(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
MyControl self = (MyControl)d;
d.Target = d.Source1 && d.Source2;
}
Run Code Online (Sandbox Code Playgroud)
第2部分:只读
internal static readonly DependencyPropertyKey TargetPropertyKey =
DependencyProperty.RegisterReadOnly(
"Target",
typeof(bool),
typeof(MyControl),
new PropertyMetadata(false));
public static readonly DependencyProperty TargetProperty =
TargetPropertyKey.DependencyProperty;
public bool Target
{
get { return (bool)GetValue(TargetProperty); }
protected set { SetValue(TargetPropertyKey, value); }
}
Run Code Online (Sandbox Code Playgroud)
免责声明:我没有尝试第2部分.
第3部分:
如果您没有定义源依赖项属性,则可以执行以下操作:
DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(
MyControl.Source1Property,
typeof(MyControl)));
if (dpd != null)
dpd.AddValueChanged(this, UpdateTarget);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4550 次 |
| 最近记录: |