WPF MultiDataTrigger Condition can't find DependencyProperty

71 Views Asked by At
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;
}

I've posted all the source code,

My problem is the MultiDataTrigger cant reach the LayoutField.LayoutType property.

I get this error on Output window when application runs. Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='StyleTest.LayoutPresenterTypeSelector', AncestorLevel='1''. BindingExpression:Path=LayoutField.FieldType; DataItem=null; target element is 'LayoutPresenterTypeSelector' (Name='PresenterSelector'); target property is 'NoTarget' (type 'Object')

Is there anything that I miss?

0

There are 0 best solutions below