WPF数据绑定到接口找不到属性 [英] WPF databinding to interface cannot find property

查看:103
本文介绍了WPF数据绑定到接口找不到属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从此处,到目前为止,这对我来说一直很好.我现在正尝试通过以下XAML执行与接口属性的数据触发绑定:

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>

当我输入此代码时,收到警告成员'SelectedItem'无法识别或无法访问",并且在尝试运行时收到类似的异常.唯一的是,接口上定义了一个 成员SelectedItem,我什至可以从XAML导航到它:

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; }
}

谁能告诉我为什么会这样,以及我可以采取什么措施来解决?如果可能的话,我想基于接口定义或为IChartDefinitionViewModel实现使用数据模板来进行管理.

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.

更新:这也行不通,但是由于另一个原因-当我尝试直接绑定到对象时,尽管HasErrors从true切换为false,背景也不会改变.

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>

推荐答案

我的原始帖子似乎存在两个问题.首先是存在数据上下文问题-感谢James Durda指出了这一点.行上下文的类型为ChartDefinitionViewModel,因此此代码可以按需工作:

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>

但是,有趣的是,绑定到IChartDefinitionViewModel接口上的HasErrors属性会导致抛出异常,说明该属性路径无效,这使我再次回到原来的查询中:

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>

但是,当我绑定到直接定义HasErrors属性的接口时,事情开始按预期工作.

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>  

我不确定这是否是一个孤立的情况,但是至少在这种情况下,WPF绑定似乎无法找到在接口继承层次结构中向上定义的属性.

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.

这篇关于WPF数据绑定到接口找不到属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆