2017-07-16 36 views
0

我将ReactiveList绑定到视图代码隐藏中的ComboBox并获取错误System.Exception:'无法找到'Value1'的视图。将ReactiveList绑定到ComboBox无法找到视图错误

ViewModel.cs

public class SourceItem 
{ 
    public override string ToString() 
    { 
     return Name; 
    } 
    public string Name { get; set; } 
} 

public class ViewModel : ReactiveObject 
{ 
    public ReactiveList<SourceItem> SourceList { get; } = new ReactiveList<SourceItem>(); 
    public SourceItem SelectedSourceItem { get; set; } 

    public ViewModel() 
    { 
     SourceList.Add(new SourceItem() {Name = "Value1"}); 
    } 
} 

View.xaml

<ComboBox Name="Source"/> 

View.cs

this.OneWayBind(ViewModel, x => x.SourceList, x => x.Source.ItemSource); 
this.Bind(ViewModel, x => x.SelectedSourceItem, x => x.Source.SelectedItem); 

是否有SIMP le方式强制ToString()用于显示值?

+0

您需要DataTemplate for ComboBox – Coding4Fun

回答

3

定期Binding将自动工作,没有DataTemplate。它将生成一个DataTemplate来显示所提供数据的表示形式string

RxUI绑定不起作用;你必须为他们提供一个DataTemplate工作:

<ComboBox Name="Source"> 
    <ComboBox.ItemTemplate> 
     <DataTemplate> 
      <TextBlock Text="{Binding}"/> 
     </DataTemplate> 
    </ComboBox.ItemTemplate> 
</ComboBox> 

如果只是用{Binding}应该退回到您的通话类ToString()。或者,您可以告诉它手动绑定到Name属性:

<TextBlock Text="{Binding Name}"/>