I have a DataGrid I want to edit. One column is a Combobox
<DataGrid ItemsSource="{Binding Persons, Mode=TwoWay}"
SelectedItem="{Binding SelectedPerson, Mode=TwoWay}" >
<DataGrid.Columns>
<DataGridTemplateColumn Header="Company" >
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=DataContext.Companies}"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Company.Name}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
The problem is if the Company is null
then I get the error
Two-way binding requires Path or XPath
How can I get around that to allow the Company be set to null?
Text="{Binding Company.Name}"
always requiresCompany
to be a valid object because the system wants to finally access theName
property. I still have some hope you can create a dummy Company object and set the Name to an empty string. Otherwise a converter helps:The converter uses reflexion to access the
Name
property. If the type ofCompany
is accessible for the converter (and the logic allows it) you can of course use is directly.The question already has a correct answer, so this is just a quick follow up. Let's assume a demo ViewModel:
Now, what is not working in the ComboBox is that it is not changing the value of a Person's Company after a new value of a different Company is selected from the ComboBox: in the demo let's say we want assign both persons1 and 2 to companyA from the UI.
Furthermore CompanyB is not selected in the ComboBox of person2, if we don't bind the
SelectedItem
of the ComboBox to the Company property of the PersonType.Here you find the fix to the Xaml of the Combobox
The classes of my demo
Please notice also that, due to the binding of Combobox
SelectedItem
, person2 has its ComboBox item CompanyB selected in the beginning and now it is possible to change one's Company to CompanyA, for example.