我需要我的控件从 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 更改时,每个 PipeContorl 都会调用 OnIsEnabledPropertyCoerce,永远不会调用 OnIsEnabledPropertyChanged,OnIsEnabledProerty Coerce 中的 ValueSource 为“默认”(显示 Coerce 始终获取默认的 false 值)。
我必须以我需要使用 Inheritance 的方式错过一些东西,我希望值源是“Inherited”并且 OnIsEnabledPropertyChanged 被调用。