Binding between the two columns in the DataGrid
I have a DataGrid which is binded to a list with two properties.
First column in the DataGrid i开发者_StackOverflow中文版s DataGridTemplateColumn and it has ComboBox inside.
Second column in the DataGrid is DataGridTextColumn and it is binded with a converter.
Whenever i change the combobox value in the first column, the second column converter must be triggered. So based on some calculation in the converter i can return the value to second column
how to do this?
You can route that through your bound source, bind the selected item of combobox to your bound data and then bind the selected object to column 2 using converter. Share the selecteditem between two columns and for the second column use the converter in binding, so it gets triggered.
Corrected copy paste error in column 2 binding,
Something like this,
Presenter / ViewModel
class DataSource : INotifyPropertyChanged {
// raise PropertyChanged when required
public ObservableCollection<string> Columns1Values {get;set;}
// raise PropertyChanged when required
public string SelectedColumn1Value {get; set;}
}
View
<dg:DataGrid
Name="sampleDG"
ItemsSource={Binding DataSourceObject}>
<dg:DataGridWidget.Columns>
<dg:DataGridTemplateColumn
Header="Column 1">
<datagrid:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label Content="{Binding Path=SelectedColumn1Value, Mode=TwoWay}"/>
</DataTemplate>
</datagrid:DataGridTemplateColumn.CellTemplate>
<datagrid:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox
ItemsSource="{Binding Path=Column1, Mode=TwoWay}"
SelectedItem="{Binding Path=SelectedColumn1Value, Mode=TwoWay}"
/>
</DataTemplate>
</datagrid:DataGridTemplateColumn.CellEditingTemplate>
</dg:DataGridTemplateColumn>
<dg:DataGridTextColumn
Header="Column 2"
Binding="{Binding Path=SelectedColumn1Value, Converter={StaticResource selectedConverter}, Mode=TwoWay}}">
</dg:DataGridTextColumn>
</dg:DataGridWidget.Columns>
</dg:DataGrid>
精彩评论