2016-03-06 28 views
0

我在我的xaml中使用ComboBox,但是我无法看到视图上的任何数据。它只显示空文本文件和空白下拉菜单。如何调试并解决ComboBox的绑定问题?

我试图在these tips的帮助下调试该问题。但是,我一直无法解决这个问题。

这里的databindingDebugConverter

public class DatabindingDebugConverter : IValueConverter 
{ 
    public object Convert(object value1, Type targetType, object parameter, CultureInfo culture) 
    { 
     Debugger.Break(); 
     return value1; 
    } 

    public object ConvertBack(object value2, Type targetType, object parameter, CultureInfo culture) 
    { 
     Debugger.Break(); 
     return value2; 
    } 
} 
  • 数值1的组合框Text=情况下返回"Field Device"(object{string})
  • 并在ItemsSource=数值1返回object{Device}Category领域和借鉴Category1持有CategoryId的对象。
  • 对于SelectedValue a "Field Device"(object{string})再次返回。

这里是ComboBox XAML:

<ComboBox x:Name="ProductCategoryComboBox" HorizontalAlignment="Right" Height="21.96" Margin="0,20,10.5,0" VerticalAlignment="Top" Width="100" 
      Text="{Binding DeviceDatabaseViewModel.SelectedDevice.Category, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource debugConverter}}" 
      IsEditable="False" 
      ItemsSource="{Binding DeviceDatabaseViewModel.SelectedDevice, Converter={StaticResource debugConverter}}" 
      SelectedValue="{Binding DeviceDatabaseViewModel.SelectedDevice.Category, Mode=TwoWay, Converter={StaticResource debugConverter}}" 
      SelectedValuePath="CategoryId" 
      DisplayMemberPath="Category" /> 

相似的结合,包括XAML中TextBlock领域工作正常,并从SelectedDevice显示字符串值。

编辑

selectedDevicedataGrid简称:

private Device _selectedDevice; 
public Device SelectedDevice 
{ 
    get 
    { 
     return _selectedDevice; 
    } 
    set 
    { 
     if (_selectedDevice == value) 
     { 
      return; 
     } 
     _selectedDevice = value; 
     RaisePropertyChanged("SelectedDevice"); 
    } 
} 

回答

1

一个ComboBox是选择一个项目从集合中(例如列表或的ObservableCollection,如果你想在UI识别收集中的变化)。 你不绑定到一个集合在这里:

的ItemsSource = “{结合DeviceDatabaseViewModel.SelectedDevice,转换器= {StaticResource的debugConverter}}”

而不是绑定到SelectedDevice你需要绑定到一个ObservableCollection AllDevices或者你可以绑定ItemsSource的东西。

这里的东西,你可以绑定一个例子:

public class DeviceDatabaseViewModel 
{ 
    public ObservableCollection<Device> AllDevices 
    { 
     get; set; 
    } 

    public DeviceDatabaseViewModel() 
    { 
     AllDevices = new ObservableCollection<Device>(); 
     AllDevices.Add(new Device { Category = 'Computer', CategoryId = 1 }, new Device { Category = 'Tablet', CategoryId = 2 }); 
    } 
} 

然后使用以下绑定:

ItemsSource="{Binding DeviceDatabaseViewModel.AllDevices, Converter= {StaticResource debugConverter}}" 
+0

啊对了,忘了,我需要收集的'itemsSource' – ajr