Reputation: 2375
C#, WPF, bound DataGrid Two combobox columns, both bound to some datasources, What I want: When users select an item from one combobox column, the selected value and items list for other combobox column will change automatically. What I saw now, datasource on the second column change, and so does selected value. but UI shows empty as if there is no selection. When I click the second column, the correct selected value will show up. I feel I like an event trigger when the selection of the first combobox changes, inform the second combobox. but not sure how to implement it. thanks
<dg:DataGrid Grid.Row="1" x:Name="basketDG" Margin="5 0 5 0" Background="White"
AutoGenerateColumns="False"
Style="{StaticResource DataGridStyle}"
SelectionMode="Extended"
GridLinesVisibility="None"
HeadersVisibility="Column" RowDetailsVisibilityMode="VisibleWhenSelected"
ItemsSource="{Binding BasketItems, Mode=OneWay}" CanUserAddRows="False" CanUserDeleteRows="False"
SelectionUnit="FullRow" SelectedItem="{Binding SelectedRelComplete}"
VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto"
SelectionChanged="BasketDgSelectionChanged"
Drop="DataGridDrop"
DragOver="DataGridDragOver"
PreviewMouseUp="DGMouseUpEvent"
AllowDrop="True"
ContextMenuOpening="basketDG_ContextMenuOpening"
>
<!-- Column definition -->
<dg:DataGridComboBoxColumn Width="200" Header="Column"
SelectedValueBinding="{Binding Path=ObjParams.ColumnName, UpdateSourceTrigger=PropertyChanged,
diagnostics:PresentationTraceSources.TraceLevel= High}"
DisplayMemberPath="ColName"
SelectedValuePath="ColName">
<dg:DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding Obj.Columns}" />
</Style>
</dg:DataGridComboBoxColumn.ElementStyle>
<dg:DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding Obj.Columns}" />
</Style>
</dg:DataGridComboBoxColumn.EditingElementStyle>
</dg:DataGridComboBoxColumn>
<dg:DataGridComboBoxColumn Header="Time Unit" SelectedValueBinding="{Binding ObjParams.TimeUnit}">
<dg:DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding TimeUnitList}" />
</Style>
</dg:DataGridComboBoxColumn.ElementStyle>
<dg:DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding TimeUnitList}" />
</Style>
</dg:DataGridComboBoxColumn.EditingElementStyle>
</dg:DataGridComboBoxColumn>
</dg:DataGrid.Columns>
</dg:DataGrid>
Upvotes: 0
Views: 1213
Reputation: 2323
Whenever you bind to another element you should use:
{Binding ElementName=sourceElementName, Path=sourceElementProperty}
Also consider direction. If the source will be the only one doing the updating, then add:
{Binding ElementName=sourceElementName, Path=sourceElementProperty, Mode=OneWay}
to that bind. Or you can update both directions, use:
{Binding ElementName=sourceElementName, Path=sourceElementProperty, Mode=TwoWay}
If you are looking to utilize pure MVVM, then have both controls bind to the same property, and only permit your source element the Mode=TwoWay privilege.
John Papa says it pretty well.
Data Binding in WPF http://msdn.microsoft.com/en-us/magazine/cc163299.aspx
Upvotes: 0