简体   繁体   English

WPF数据绑定到接口找不到属性

[英]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: 我现在正尝试通过以下XAML执行与接口属性的datatrigger绑定:

<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. 当我输入此代码时,收到警告“成员'SelectedItem'无法识别或不可访问”,并且在尝试运行时收到类似的异常。 Only thing is, there is a member SelectedItem defined on the interface, and I can even navigate to it from the XAML: 唯一的一点是, 接口上定义的成员的SelectedItem,我甚至可以从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. 如果可能的话,我想基于接口定义或对IChartDefinitionViewModel实现使用数据模板进行管理。

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. 更新:这也行不通,但是由于另一个原因-当我尝试直接绑定到对象时,尽管HasErrors从true切换为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>

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. 首先是存在数据上下文问题-感谢James Durda指出了这一点。 The row context is of type ChartDefinitionViewModel, so this code works as desired: 行上下文的类型为ChartDefinitionViewModel,因此此代码可以按需运行:

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

<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. 但是,当我绑定到直接定义HasErrors属性的接口时,事情开始按预期工作。

<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. 我不确定这是否是一个孤立的情况,但是至少在这种情况下,WPF绑定似乎无法定位在接口继承层次结构中向上定义的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM