我正在尝试将datagridcombobox选定值绑定到expando对象中的属性值。但是,我无法做到这一点。也许任何人都可以给我一些关于这个问题的见解。
以下是代码的相关位和和平: 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。我已经将Dictionary绑定到组合框,这意味着当我单击组合框时我得到了元素,但它没有将数值绑定到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 :(得分:0)
因此,我自己发现了这个问题。类似于这篇文章中描述:Binding ItemsSource of a ComboBoxColumn in WPF DataGrid
主要的缺陷是尽管ItemsSource绑定的数据上下文在datagridcombobox中混乱,但SelectedValueBinding仍然保留了在Datagrid ItemsSource中设置的默认数据上下文,因此在创建时不会指定集合的完整路径。 SelectedValueBinding应该使用原始ItemsSource中数据网格的相对路径。