kzub
kzub

Reputation: 232

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.

Upvotes: 1

Views: 2305

Answers (3)

Manish kosta
Manish kosta

Reputation: 91

I think there is another good way to accomplish your goals:

<DataGrid Name="dgResults" ItemsSource="{Binding Path=DataGridObj}"
SelectedItem="{Binding SelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

Upvotes: 2

kzub
kzub

Reputation: 232

Т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>

Upvotes: 1

Remy
Remy

Reputation: 252

Maybe your binding is wrong. If you get your items in the grid, your ItemsSource is fine. Use the SelectedValue and set the SelectedValuePath to the column you want the data from.

Skip the multibinding and set the binding on the combobox to be set to the SelectedValue of the DataGrid.

<DataGrid Name="dgResults" ItemsSource="{Binding Path=DataGridObj}"
SelectedItem="{Binding SelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectedValue="{Binding SelectedValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectedValuePath="ItemNoX"

Upvotes: 1

Related Questions