user2412341
user2412341

Reputation: 23

WPF Datagrid MVVM : Combobox Binding using DatagridTemplateColumn

Have a WPFDatagrid binded to combobox using Datagridtemplatecolumn. Finding difficult to get the selectedItem of the combobox binding. Have found similar examples around but that's not resolving my problem.

Please find the code snippet of my XAML and the data structure below:

public class X
{
    public X ()
    {
        abc = new ObservableCollection<P>();
    }

    public ObservableCollection<P> Y
    {
        get { return abc; }
        set { abc = value; PropertyChanged("Y"); }
    }
}

public class P
{
    private string id;
    private string name;

    public string ID
    {
        get
        {
            return id;
        }
        set
        {
            id = value;
            InvokePropertyChanged("ID");
        }
    }

    public string Name
    {
        get
        {
            return name;
        }
        set
        {
            name = value;
            InvokePropertyChanged("Name");
        }
    }
}

I have a datastructure defined above that implements the INotifyPropertychanged interface.

    <controls:DataGrid Name="datagrid" AutoGenerateColumns="False" ItemsSource="{Binding XList}" Grid.Row="0"
                   SelectedItem="{Binding SelectedX, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">

        <controls:DataGrid.Columns>
            <controls:DataGridTemplateColumn Header="Yl">
                <controls:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ComboBox ItemsSource="{Binding Y}" 
                                  IsSynchronizedWithCurrentItem="False" DisplayMemberPath="Name"
                                  SelectedValue="{Binding Path=SelectedY, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnTargetUpdated=True}"
                                  SelectedValuePath="SelectedY" 
                                  SelectedItem="{Binding SelectedY, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"  />
                    </DataTemplate>
                </controls:DataGridTemplateColumn.CellTemplate>
            </controls:DataGridTemplateColumn>
        </controls:DataGrid.Columns>
    </controls:DataGrid>

Now, in view model, have a observablecollection of List of X i.e., XList and that is binded to the datagrid in the XAML. and have Y within each row of the datagrid binded to the Combobox. Have a property as SelectedY, binded to the SelectedItem of the combobox. Have also a property as SelectedX binded to the selectedItem of the datagrid, which works fine.

Issue is not able to get the Selected Item binding of the Combobox. Not able to set the selected item for the combobox when the selection has changed.

Can anybody help me out to set the selecteditem binding of the combo box?

Upvotes: 0

Views: 854

Answers (1)

FrsECM
FrsECM

Reputation: 245

Where is set your datacontext ? You can do something like that :

<controls:UserControl x:Name=MainControl>
     <controls:UserControl.DataContext>
        <classpath:X/>
     </controls:UserControl.DataContext>
     <controls:DataGrid ItemsSource={Binding YourItemsContainer}>
          <controls:DataGrid.Columns>
               <controls:DataGridComboBoxColumn ItemsSource={Binding ElementName=MainControl,Path=DataContext.Y}
                                       SelectedItem={Binding ElementName=MainControl,Path=DataContext.SelectedY}
DisplayMemberPath=Name />
          </controls:DataGrid.Columns>
     </controls:DataGrid>
</controls:UserControl>

The idea is to set a name to the root element connected to your datacontext, then you can access to it's datacontext property easily by the path. When you are inside of a template, the datacontext is the ItemsSource objects.

Hope it will help you a little !

Upvotes: 0

Related Questions