WPF:在GridViewColumn中为DisplayMemberBinding使用PriorityBinding

时间:2016-08-31 06:44:22

标签: c# wpf data-binding

我在WPF中为DisplayMemberBinding设置了GridViewColumn

<GridViewColumn Header="{x:Static resx:TextResources.ExcutionTimesHeaderErrorType}" DisplayMemberBinding="{Binding Exception, Mode=OneWay, Converter={StaticResource typeConverter}, FallbackValue='---'}"/>

typeConverter&#39;模仿&#39; Exception.GetType()扩展名,在Gui中将异常类型显示为字符串。如果没有异常且该值为null,我想在此列中输出另一个属性。现在我想对PriorityBinding使用DisplayMemberBinding,但GridViewColumn除了PriorityBinding以外都没有作为&#39;子元素&#39;,也不可能将DisplayMemberBinding属性用作&#39;子元素&#39;。有没有办法PriorityBinding使用GridViewColumn这样的方式:

<GridViewColumn Header="{x:Static resx:TextResources.ExcutionTimesHeaderErrorType}">
    <PriorityBinding>
        <Binding Path="Exception" Mode="OneWay" Converter="{StaticResource typeConverter}" IsAsync="True" />
        <Binding Path="AnotherProperty" IsAsync="True" />
    </PriorityBinding>
</GridViewColumn>

修改

我将我的代码更改为&#34; ASh&#34;下面,但它不起作用:

<GridViewColumn Header="{x:Static resx:TextResources.ExcutionTimesHeaderErrorType}">
    <GridViewColumn.DisplayMemberBinding>
        <PriorityBinding  FallbackValue="-*-">
            <Binding Path="Exception" Converter="{StaticResource typeConverter}" Mode="OneWay" IsAsync="True"/>
            <Binding Path="Comment" FallbackValue="---"/>
        </PriorityBinding>
    </GridViewColumn.DisplayMemberBinding>
</GridViewColumn>

如果Exception属性为null,则显示值为空。我检查了第二个绑定属性&#34;评论&#34;设置好了。

这两个绑定正常工作:

<GridViewColumn Header="{x:Static resx:TextResources.ExcutionTimesHeaderComment}" DisplayMemberBinding="{Binding Comment, FallbackValue='---'}"/>

<GridViewColumn Header="test2" DisplayMemberBinding="{Binding Exception, Mode=OneWay, Converter={StaticResource typeConverter}, FallbackValue='---'}"/>

1 个答案:

答案 0 :(得分:1)

您缺少<GridViewColumn.DisplayMemberBinding>代码:

<GridViewColumn>
    <GridViewColumn.DisplayMemberBinding>
        <PriorityBinding>
           <Binding Path="Exception" Mode="OneWay" Converter="{StaticResource typeConverter}" IsAsync="True" />
           <Binding Path="AnotherProperty" IsAsync="True" />
        </PriorityBinding>
    </GridViewColumn.DisplayMemberBinding>
</GridViewColumn>

GridViewColumn标有ContentProperty属性,内容属性为标题:[ContentProperty("Header")]。因此,<GridViewColumn>标记没有GridViewColumn.前缀的内部内容应该分配给标题属性