【问题标题】:WPF Datagrid - RowDetails binding to optional property?WPF Datagrid - RowDetails 绑定到可选属性?
【发布时间】:2013-02-28 15:41:51
【问题描述】:

我正在尝试设计一个 WPF 数据网格,其中 RowDetails(一个文本块)将绑定到网格的 ItemSource 元素的 Details 字符串属性。

   <DataGrid.RowDetailsTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Details}" .....
            </DataTemplate>
        </DataGrid.RowDetailsTemplate>

诀窍是,绑定集合中的元素可能没有Details 属性,在这种情况下,RowDetails 应该是空的。有没有办法让 WPF 绑定到在运行时甚至可能不存在的属性,而不会引发异常? (需要明确的是,绑定集合中的项目要么都具有Details 属性,要么都没有。我说的不是包含多个对象类型的集合。)

我想一个答案是强制此网格的用户提供始终具有 Details 属性(IDetails 接口或其他)的对象,但我认为不必这样做会很好 - 我想要网格在它可以显示的对象中非常灵活。

【问题讨论】:

    标签: wpf binding datagrid rowdetails


    【解决方案1】:

    环顾四周后,我发现了 PriorityBindings,它可以完成工作。这里我绑定到 RowDetailsTemplate 中的 TextBlock,如果找不到绑定的属性,则将 Fallbackvalue 设置为空字符串:

    <TextBlock.Text>
        <PriorityBinding FallbackValue="">
               <Binding Path="Details" IsAsync="True"/>
        </PriorityBinding>
    </TextBlock.Text>
    

    【讨论】:

      猜你喜欢
      • 2010-12-01
      • 2016-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-03
      • 2013-09-17
      • 2017-09-18
      相关资源
      最近更新 更多