【问题标题】:Bind DisplayMemberPath to a function in WPF将 DisplayMemberPath 绑定到 WPF 中的函数
【发布时间】:2010-08-31 16:04:11
【问题描述】:

有没有办法可以将组合框的DisplayMemberPath 绑定到一个函数?我目前正在使用的对象具有三个我希望用于演示的属性; FirstNameLastNameMiddleName

我在 Formatting 类中编写了一个静态方法来处理它。这个方法叫做FullName,接受三个字符串参数。

有没有一种方法可以调用 Formatting.FullName 并从我的 Person 对象中传递三个参数,以便将格式化的全名显示到组合框项目中?

我已将以下 XAML 添加到页面的资源部分:

<ObjectDataProvider ObjectType="{x:Type business:Formatting}" x:Key="formatter" />

<ObjectDataProvider ObjectInstance="{StaticResource formatter}" 
                    MethodName="FullName" 
                    x:Key="nameFormatter">
    <ObjectDataProvider.MethodParameters>
        <system:String>Bloggs</system:String>
        <system:String>Joe</system:String>
        <system:String>Q</system:String>
    </ObjectDataProvider.MethodParameters>
</ObjectDataProvider>

我的组合框 XAML 现在看起来像这样:

<ComboBox Height="23" Width="120" 
          HorizontalAlignment="Left" VerticalAlignment="Top" 
          Margin="467,72,0,0" 
          Name="cboDistrictAttorney" 
          SelectedValuePath="Id" 
          SelectedValue="{Binding DistrictAttorneyId}" 
          DisplayMemberPath="{Binding Source={StaticResource nameFormatter}}" />

最终结果是我有一个组合框,里面装满了空白项。

【问题讨论】:

    标签: c# wpf function binding formatting


    【解决方案1】:

    另一种情况是 DataTemplate,但 DataTemplate 对您的应用程序来说更重。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多