WPF databinding to interface cannot find property

五迷三道 提交于 2021-02-11 13:14:24

问题


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>

回答1:


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.



来源:https://stackoverflow.com/questions/36578799/wpf-databinding-to-interface-cannot-find-property

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!