【发布时间】:2013-04-01 20:21:42
【问题描述】:
我需要我的控件从 Grid 类型的祖先继承 UIElement.IsEnabledProperty(可选 Window 或任何其他我可以用来包装我的网格的元素)
CS : 下面我覆盖 UIElement.IsEnabledProperty 的元数据并使用 Change 和 Coerce delegates 设置它。
static PipeControl()
{
PipeControl.IsEnabledProperty.OverrideMetadata(typeof(PipeControl), new FrameworkPropertyMetadata(false, OnIsEnabledPropertyChanged, OnIsEnabledPropertyCoerce));
}
private static void OnIsEnabledPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var isEnabled = (bool)e.NewValue;
}
private static object OnIsEnabledPropertyCoerce(DependencyObject d, object baseValue)
{
var valueSource = DependencyPropertyHelper.GetValueSource(d, PipeControl.IsEnabledProperty);
var pipeContorl = d as PipeControl;
if (pipeContorl == null) return baseValue;
return (bool)baseValue && pipeContorl.IsMyPipe;
}
XAML:
<Grid IsEnabled="{Binding IsMyCondition , Mode=OneWay}">
<game:PipeControl Grid.Row="2" />
<game:PipeControl Grid.Row="2" Grid.Column="1" />
</Grid>
每次 IsMyCondition 更改 OnIsEnabledPropertyCoerce 在每个 PipeContorl 中调用, OnIsEnabledPropertyChanged 永远不会被调用, OnIsEnabledProerty Coerce 中的 ValueSource 为“默认”(显示 Coerce 始终获取默认的 false 值)。
我必须以我需要使用继承的方式错过一些东西, 我希望价值源是“继承的” 和要调用的 OnIsEnabledPropertyChanged。
【问题讨论】:
标签: wpf inheritance custom-controls dependency-properties