0

I am trying to bind a datagridcombobox selected value to a property value in an expando object. However, I am not managing to do that. Maybe anyone can give me some insights into the problem.

Here are the relevant bits and peaces of the code: XAML datagrid

<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>

View Model:

    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; }
    }

The data in the expando object property is of type uint. I have gotten the Dictionary to bind to the combo box, meaning that I get the elements when I click on the combo box, however it does not bind the numeric value to the ExpandoObject. However the values in the CheckBoxColumns and the TextBoxColumns are binding succesfully, thus I'm a bit confused why it does not bind in the case of ComboBox.

The error that I get is:

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)'
xnonamex
  • 405
  • 2
  • 5
  • 17

1 Answers1

0

So eventyaly found the problem myself. Similar described in this post: Binding ItemsSource of a ComboBoxColumn in WPF DataGrid

The main pitfall was that although the data context of ItemsSource binding gets jumbled up in the datagridcombobox the SelectedValueBinding still keeps the default data context that was set in the Datagrid ItemsSource, thus instead of specifying the full path to your collection when crating the SelectedValueBinding the relative path within the original ItemsSource for the datagrid should be used.

Community
  • 1
  • 1
xnonamex
  • 405
  • 2
  • 5
  • 17