WPF DisplayMemberProperty无法按预期在后面的代码中运行

时间:2018-10-12 14:24:51

标签: c# wpf vb.net xaml bind

我有一个简单的ObservableCollection我要绑定在代码中的Person类。除了DisplayMemberProperty以外,其他所有东西都绑定正常,DD.DisplayMember = "Lastname"在下拉列表中选择一个项目时,仅在下拉列表的文本部分显示Null。如果我添加<Syncfusion:SfMultiColumnDropDownControl x:Name="DD" HorizontalAlignment="Left" VerticalAlignment="Top" Width="300" Margin="20"/> 可以正常工作,但是由于我需要在运行时添加控件,因此并不是全部都是下拉菜单,我需要使它作为绑定起作用。

我的XAML:

With OC
    .Add(New Person With {.Id = "1", .Lastname = "Smith"})
    .Add(New Person With {.Id = "2", .Lastname = "Jones"})
    .Add(New Person With {.Id = "3", .Lastname = "Johnson"})
End With

Dim ItemsSourceBinding As New Binding
With ItemsSourceBinding
    .Source = OC
    .Mode = BindingMode.OneWay
    .UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
End With
BindingOperations.SetBinding(DD, SfMultiColumnDropDownControl.ItemsSourceProperty, ItemsSourceBinding)

Dim SelectedItemBinding As New Binding
With SelectedItemBinding
    .Source = Report
    .Path = New PropertyPath("MyPerson")
    .Mode = BindingMode.TwoWay
    .UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged 
End With
BindingOperations.SetBinding(DD, SfMultiColumnDropDownControl.SelectedItemProperty, SelectedItemBinding)

Dim DisplayMemberBinding As New Binding
With DisplayMemberBinding
    .Source = OC
    .Path = New PropertyPath("Lastname")
    .Mode = BindingMode.OneWay
    .UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
End With
BindingOperations.SetBinding(DD, SfMultiColumnDropDownControl.DisplayMemberProperty, DisplayMemberBinding)

我的代码:

LinearLayout
  --> ImageButton
  --> TextView
  --> Button

1 个答案:

答案 0 :(得分:0)

不应该绑定DisplayMemberProperty。应该将其设置为属性的名称,即string

DD.DisplayMemberProperty = "LastName"

如果将其绑定到源属性,则需要确保此属性返回string“姓氏”,而不是Lastname属性的 value