2014-09-30 58 views
0

我想將datagridcombobox選定值綁定到expando對象中的屬性值。但是,我沒有設法做到這一點。也許任何人都可以給我一些關於這個問題的見解。Datagridcombobox綁定與expandoobject

下面是相關位和代碼的一塊塊: XAML數據網格

<DataGrid x:Name="RXSignalsDataGrid" Grid.Column="1" CanUserReorderColumns="True" CanUserAddRows="False" CanUserResizeColumns="True" 
          CanUserSortColumns="False" Margin="5" ItemsSource="{Binding CANRXFrameSignals}" AutoGenerateColumns="False"> 
    <DataGrid.Columns> 
    <DataGridCheckBoxColumn Header="Enabled" Binding="{Binding Property_0_Value}" Width="Auto" IsReadOnly="False"/> 
    <DataGridTextColumn Header="Start Bit" Binding="{Binding Property_2_Value}" Width="Auto" IsReadOnly="False"/> 
    <DataGridTextColumn Header="Length" Binding="{Binding Property_5_Value}" Width="Auto" IsReadOnly="False"/> 
    <DataGridCheckBoxColumn Header="Little Endian" Binding="{Binding Property_1_Value}" Width="Auto" IsReadOnly="False"/> 
    <DataGridCheckBoxColumn Header="Conversion 1 Enabled" Binding="{Binding Property_4_Value}" Width="Auto" IsReadOnly="False"/> 
    <DataGridTextColumn Header="Conversion 1 Type" Binding="{Binding Property_7_Value}" Width="Auto" IsReadOnly="False"/> 
    <DataGridComboBoxColumn Header="Conversion 1 Type" Width="Auto" IsReadOnly="False" DisplayMemberPath="Key" SelectedValuePath="Value"> 
     <DataGridComboBoxColumn.ElementStyle> 
      <Style> 
       <Setter Property="ComboBox.ItemsSource" Value="{Binding Path=SignalConversionOperators}"/> 
       <Setter Property="ComboBox.SelectedValue" Value="{Binding Path=CANRXFrameSignals.Property_7_Value}"/> 
      </Style> 
     </DataGridComboBoxColumn.ElementStyle> 
     <DataGridComboBoxColumn.EditingElementStyle> 
      <Style> 
       <Setter Property="ComboBox.ItemsSource" Value="{Binding Path=SignalConversionOperators}"/> 
       <Setter Property="ComboBox.SelectedValue" Value="{Binding Path=CANRXFrameSignals.Property_7_Value}"/> 
      </Style> 
     </DataGridComboBoxColumn.EditingElementStyle> 
    </DataGridComboBoxColumn> 
    <DataGridTextColumn Header="Conversion 1 Value" Binding="{Binding Property_8_Value}" Width="Auto" IsReadOnly="False"/> 
    <DataGridCheckBoxColumn Header="Conversion 2 Enabled" Binding="{Binding Property_3_Value}" Width="Auto" IsReadOnly="False"/> 
    <DataGridTextColumn Header="Conversion 2 Type" Binding="{Binding Property_6_Value}" Width="Auto" IsReadOnly="False"/> 
    <DataGridTextColumn Header="Conversion 2 Value" Binding="{Binding Property_9_Value}" Width="Auto" IsReadOnly="False"/> 
    <DataGridTextColumn Header="Variable" Binding="{Binding Property_10_Value}" Width="Auto" IsReadOnly="False"/> 
    </DataGrid.Columns> 
</DataGrid> 

視圖模型:

private ObservableCollection<ExpandoObject> canRXFrameSignals; 
    private Dictionary<string, uint> signalConversionOperators = new Dictionary<string, uint>(){ 
     {"+",0}, 
     {"-", 1}, 
     {"*", 2}, 
     {"/", 3}, 
     {"AND", 4}, 
     {"OR", 5}, 
     {"XOR", 6}, 
     {"Power", 7}, 
     {"S +", 8}, 
     {"S -", 9}, 
     {"S *", 10}, 
     {"S /", 11}, 
     {">>", 12}, 
    }; 

    public ObservableCollection<ExpandoObject> CANRXFrameSignals 
    { 
     get 
     { 
      return canRXFrameSignals; 
     } 
     private set 
     { 
      if (canRXFrameSignals == value) 
      { 
       return; 
      } 
      canRXFrameSignals = value; 
      OnPropertyChanged("CANRXFrameSignals"); 
     } 
    } 

    public Dictionary<string, uint> SignalConversionOperators 
    { 
     get { return signalConversionOperators; } 
    } 

在的expando對象屬性的數據是uint類型。我已經將字典綁定到組合框,這意味着當我單擊組合框時,將獲取元素,但是它不會將數字值綁定到ExpandoObject。然而,CheckBoxColumns和TextBoxColumns中的值是成功綁定的,因此我有點困惑它爲什麼不綁定在ComboBox的情況下。

,我得到的錯誤是:

A first chance exception of type 'System.InvalidOperationException' occurred in System.Core.dll 
System.Windows.Data Error: 17 : Cannot get 'CANRXFrameSignals' value (type 'Object') from '' (type 'ExpandoObject'). BindingExpression:Path=CANRXFrameSignals.Property_7_Value; DataItem='ExpandoObject' (HashCode=8435281); target element is 'TextBlockComboBox' (Name=''); target property is 'SelectedValue' (type 'Object') InvalidOperationException:'System.InvalidOperationException: Property path is not valid. 'System.Dynamic.ExpandoObject+MetaExpando' does not have a public property named 'Items'. 
     at CallSite.Target(Closure , CallSite , Object) 
     at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0) 
     at MS.Internal.DynamicPropertyAccessorImpl.GetValue(Object component) 
     at MS.Internal.Data.PropertyPathWorker.GetValue(Object item, Int32 level) 
     at MS.Internal.Data.PropertyPathWorker.RawValue(Int32 k)' 

回答

0

所以eventyaly發現自己的問題。類似的在這個職位描述:Binding ItemsSource of a ComboBoxColumn in WPF DataGrid

主要缺陷是,雖然ItemsSource時的數據上下文綁定都在datagridcombobox錯雜的SelectedValueBinding仍然保持這是在DataGrid的ItemsSource設置,因而不是指定的默認數據上下文在使用SelectedValueBinding時,應使用原始ItemsSource中相對路徑的DataGrid的完整路徑。