WPF MultiDataTrigger条件无法找到DependencyProperty

时间:2018-03-31 07:54:53

标签: wpf multidatatrigger

internal class LayoutPresenterTypeSelector : Control
{
    public ICommand ClearAreaCommand
    {
        get { return (ICommand)GetValue(ClearAreaCommandProperty); }
        set { SetValue(ClearAreaCommandProperty, value); }
    }

    public WrapFieldBase LayoutField
    {
        get { return (WrapFieldBase)GetValue(LayoutFieldProperty); }
        set { SetValue(LayoutFieldProperty, value); }
    }

    public static readonly DependencyProperty LayoutFieldProperty = DependencyProperty.Register("LayoutField", typeof(WrapFieldBase), typeof(LayoutPresenterTypeSelector), new PropertyMetadata(new WrapFieldBase() { FieldType = WrapFieldBase.FieldTypes.Empty }));
    public static readonly DependencyProperty ClearAreaCommandProperty = DependencyProperty.Register("ClearAreaCommand", typeof(ICommand), typeof(LayoutPresenterTypeSelector), new PropertyMetadata(null));
}

的ControlTemplate

<ControlTemplate x:Key="LayoutPresenterTypeSelectorControlTemplate" TargetType="{x:Type local:LayoutPresenterTypeSelector}">
<Border BorderThickness="0" Background="{TemplateBinding Background}">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

        <Button Grid.Column="5" Content="Clear All" Command="{Binding ClearAreaCommand, RelativeSource={RelativeSource AncestorType=local:LayoutPresenterTypeSelector}}" x:Name="PART_Clear" IsEnabled="False" Height="30"/>
    </Grid>
</Border>
<ControlTemplate.Triggers>
    <MultiDataTrigger>
        <MultiDataTrigger.Conditions>
            <Condition Binding="{Binding LayoutField.FieldType, RelativeSource={RelativeSource AncestorType=local:LayoutPresenterTypeSelector}}" Value="Notes"/>
        </MultiDataTrigger.Conditions>
        <Setter TargetName="PART_Clear" Property="IsEnabled" Value="true"/>
    </MultiDataTrigger>
</ControlTemplate.Triggers>

WrapFieldBase

internal class WrapFieldBase : INotifyPropertyChanged
{
    public enum FieldTypes { Notes, Order, Empty }

    public int RowIndex { get; set; }
    public int RowSpan { get; set; }

    public int ColumnIndex { get; set; }
    public int ColumnSpan { get; set; }

    public SolidColorBrush Background { get; set; }
    public FieldTypes FieldType { get; set; }

    public Thickness Margin { get; set; }

    public void NotifyPropertyChanged(string property) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
    public event PropertyChangedEventHandler PropertyChanged;
}

我发布了所有源代码,

我的问题是MultiDataTrigger无法到达LayoutField.LayoutType属性。

应用程序运行时,在输出窗口中出现此错误。 无法找到与引用'RelativeSource FindAncestor绑定的源,AncestorType ='StyleTest.LayoutPresenterTypeSelector',AncestorLevel ='1''。 BindingExpression:路径= LayoutField.FieldType;的DataItem = NULL; target元素是'LayoutPresenterTypeSelector'(Name ='PresenterSelector'); target属性为'NoTarget'(类型'Object')

有什么我想念的吗?

0 个答案:

没有答案