2016-03-06 67 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