Search code examples
wpfvb.netdatagridselecteditemmultibinding

Datagrid SelectedItem Multibinding


I need to bind the value of the SelectedItem from the datagrid to:

  1. SelectedItem of a combo box on the same page

  2. Property in the viewmodel

In other words: when I select a row in the datagrid the value in the combobox should change and value of the meant above property should be also set to the value of the selected item of the datagrid.

I tried to use multibinding like this:

<DataGrid.SelectedItem>
    <MultiBinding Converter="{StaticResource sapConverter}" >
        <Binding Path="SelectedSap" Mode="TwoWay"/>
        <Binding ElementName="cbSearchCompanyName" Path="SelectedItem" Mode="OneWay"/>                                    
    </MultiBinding>
</DataGrid.SelectedItem>

the SelectedSap here is that property, that I want to update. But when I look at the values() in the converter, value(0) corresponding to the SelectedSap is always Nothing and as a result the property doesn't change as I want. The binding with the combo works fine.

I try to test it without multibinding. I mean, I don't care about the combo, I'm just changing the value of the property. Like this:

<DataGrid.SelectedItem>
        <Binding Path="SelectedSap" Mode="TwoWay"/>
</DataGrid.SelectedItem>

everything works fine. Where is the trick and how should I implement the functionality I need? Thank you.


Solution

  • Тhank u a lot! Both your answers gave me a hint. Actually I have to bind three controls together (imagine functionality "search item" - you have a combo "search by item.X", combo "search by item.Y" and a datagrid with items), that's why I was little confused and started with the multibinding. The things are much more easier. Here's my code that now works:

    <StackPanel Orientation="Horizontal" Grid.Row="0" >                        
                        <Label Content="Search company by name:"/>
                        <ComboBox MinWidth="200" Width="Auto" Name="cbSearchCompanyName"
                                        ItemsSource="{Binding CompanyList,Mode=TwoWay}"
                                        IsSynchronizedWithCurrentItem="True" 
                                        DisplayMemberPath="CompanyName1"
                                        SelectedValuePath="Sap"
                                  SelectedItem="{Binding Path=SelectedSap, Mode=TwoWay}"
                                  SelectedValue="{Binding Path=SelectedSap.Sap, Mode=TwoWay}"/>
    
                        <Label Content="by SAP number:" />
                        <ComboBox MinWidth="200" Width="Auto" Style="{StaticResource marginStyle}" Name="cbSearchCompanySap"
                                        ItemsSource="{Binding CompanyList,Mode=TwoWay}"          
                                        IsSynchronizedWithCurrentItem="True" 
                                        DisplayMemberPath="Sap"
                                        SelectedValuePath="Sap"
                                  SelectedItem="{Binding Path=SelectedSap, Mode=TwoWay}"
                                  SelectedValue="{Binding Path=SelectedSap.Sap, Mode=TwoWay}"/>
    
                    </StackPanel>
    
                    <ScrollViewer Grid.Row="1" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
                        <DataGrid x:Name="CompanyList" AutoGenerateColumns="True" 
                                  ItemsSource="{Binding CompanyList,Mode=TwoWay}"
                                  MaxWidth="950" Height="300" Margin="0 2 0 0">                                  
                            <DataGrid.SelectedItem>
                                <Binding Path="SelectedSap" Mode="TwoWay"/>
                            </DataGrid.SelectedItem>                            
                        </DataGrid>
                    </ScrollViewer>