ket
ket

Reputation: 758

WPF databinding to interface cannot find property

I am working from the solution presented here, which has worked well for me until this point. I'm now trying to perform datatrigger binding to an interface property via the following XAML:

<DataGrid.RowStyle>
    <Style TargetType="{x:Type DataGridRow}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=(selection:IChartDefinitionViewModel.SelectedItem).HasErrors}" Value="False">
        <Setter Property="Background" Value="Transparent"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding Path=(selection:IChartDefinitionViewModel.SelectedItem).HasErrors}" Value="True">
        <Setter Property="Background" Value="Red"/>
        </DataTrigger>
    </Style.Triggers>
    </Style>
</DataGrid.RowStyle>

When I enter this code I get a warning "The member 'SelectedItem' is not recognized or is not accessible", and I get a similar exception when I try to run. Only thing is, there is a member SelectedItem defined on the interface, and I can even navigate to it from the XAML:

public interface IChartDefinitionViewModel : IReactiveSelector<SomeClass>, IMayHaveErrors
{
     // stuff
}

public interface IReactiveSelector<T> : // more stuff  
{
    T SelectedItem { get; set; }
}

Can anyone advise why this is happening and what I can do for a workaround? I'd like to manage this based on the interface definition or using a datatemplate for my IChartDefinitionViewModel implementation, if possible.

Update: This also does not work, but for a different reason - when I attempt to bind to the object directly, the background doesn't change despite the fact that HasErrors toggles from true to false.

<DataGrid.RowStyle>
    <Style TargetType="{x:Type DataGridRow}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding SelectedItem.HasErrors}" Value="True">
        <Setter Property="Background" Value="Transparent"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding SelectedItem.HasErrors}" Value="False">
        <Setter Property="Background" Value="Red"/>
        </DataTrigger>
    </Style.Triggers>
    </Style>
</DataGrid.RowStyle>

Upvotes: 0

Views: 1184

Answers (1)

ket
ket

Reputation: 758

There appear to have been two issues with my original post. The first is that there was a datacontext issue - thanks to James Durda for pointing this out. The row context is of type ChartDefinitionViewModel, so this code works as desired:

<DataGrid.RowStyle>
    <Style TargetType="{x:Type DataGridRow}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=(selection:ChartDefinitionViewModel.HasErrors)}" Value="False">
        <Setter Property="Background" Value="Transparent"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding Path=(selection:ChartDefinitionViewModel.HasErrors)}" Value="True">
        <Setter Property="Background" Value="Red"/>
        </DataTrigger>
    </Style.Triggers>
    </Style>
</DataGrid.RowStyle>

Interestingly, however, binding to the HasErrors property on the IChartDefinitionViewModel interface results in a thrown exception stating that the property path is invalid, which brings me back again to my original inquiry:

<DataGrid.RowStyle>
    <Style TargetType="{x:Type DataGridRow}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=(selection:IChartDefinitionViewModel.HasErrors)}" Value="False">
        <Setter Property="Background" Value="Transparent"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding Path=(selection:IChartDefinitionViewModel.HasErrors)}" Value="True">
        <Setter Property="Background" Value="Red"/>
        </DataTrigger>
    </Style.Triggers>
    </Style>
</DataGrid.RowStyle>

Things, however, begin to work as expected when I bind to the interface on which the HasErrors property is directly defined.

<DataGrid.RowStyle>
    <Style TargetType="{x:Type DataGridRow}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=(interfaces:IMayHaveErrors.HasErrors)}" Value="False">
        <Setter Property="Background" Value="Transparent"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding Path=(interfaces:IMayHaveErrors.HasErrors)}" Value="True">
        <Setter Property="Background" Value="Red"/>
        </DataTrigger>
    </Style.Triggers>
    </Style>
</DataGrid.RowStyle>  

I'm not sure if this is an isolated case, but it appears that WPF binding can't locate properties defined upwards in the interface inheritance hierarchy, at least in this instance.

Upvotes: 1

Related Questions