0

I want to select 1 item in ComboBox dynamically because i have many ComboBox in DataGrid row . I try using SelectedValue or SelectedIndex but it still not working . Please help me . My code here

Xaml file :

<DataGridTemplateColumn  Header="DataType_Id">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <ComboBox Loaded="cbxDataType_Loaded" Name="cbxDataType" SelectionChanged="ComboBox_SelectionChanged" 
                     SelectedValuePath="Id"
                      DisplayMemberPath="Name"
                      ItemsSource="{Binding Path=masterData}"
                      SelectedValue="{Binding Path=ComboboxObj,Mode=TwoWay}"
                      SelectedItem="{Binding Path=seletedItem}"
                      DataContext="{Binding}">
            </ComboBox>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

ComboBox Object

public class ComboboxObj
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    //public string selectedItem { get; set; }
}

DataGrid row Object

public class ListDataExtract
{
    public SP_List_Details Detail { get; set; }
    public List<ComboboxObj> masterData { get; set; }
    public ComboboxObj seletedItem { get; set; }
}

Main Process

       for (int i = 0; i < lstDetail.Count; i++)
            {
                ComboboxObj cbxObj = new ComboboxObj();
                ListDataExtract extract = new ListDataExtract();
                extract.Detail = lstDetail[i];
                extract.masterData = lstCbx;
                // Create Seleted Item
                cbxObj.Id = lstDetail[i].DataType_Id.Value;
                cbxObj.Name = findIndexMasterData(lstDetail[i].DataType_Id.Value, lstCbx);
                // lstCbx is List Object ComboboxObj
                extract.seletedItem = lstCbx[0];
                // End Create Seleted Item
                lstExtract.Add(extract);
            }
            DataGridListDetails.ItemsSource = lstExtract;
Jai
  • 8,165
  • 2
  • 21
  • 52
jonny
  • 165
  • 3
  • 13
  • Your classes have to implement INotifyPropertyChanged pattern. You can find a lot of documentation on the net. http://stackoverflow.com/questions/3505716/how-to-use-inotifypropertychanged-correctly-in-wpf-xaml – Lupu Silviu Nov 11 '16 at 07:38
  • Thank but where i can write funtion RaisePropertyChanged :( – jonny Nov 11 '16 at 08:07
  • My new issue is "Cannot evaluate expression because the current thread is in a stack overflow state" :( – jonny Nov 11 '16 at 08:13
  • Can you confirm that this is what you have done? http://stackoverflow.com/questions/1315621/implementing-inotifypropertychanged-does-a-better-way-exist It sounds like you did not create members and properties. – Lupu Silviu Nov 11 '16 at 08:53

1 Answers1

0

Sorry dude, I try to copy your code and figure out to make it work, but it just does not make sense, seems like you still have a lot to learn. You need to know what is DataBinding if you want to learn WPF.

My advice would be: start little and make it grow.

First of all: display a simple combobox containing string, populated from code behind (xaml.cs). Something like:

<Grid>
   <ComboBox x:Name="MyCombo"></ComboBox>
</Grid>

And in the code behind

    var myComboItemsSource = new List<string>();
    MyCombo.ItemsSource = myComboItemsSource;

    myComboItemsSource.Add("hello");
    myComboItemsSource.Add("world");

Then do it with databinding:

<ComboBox ItemsSource="{Binding MyComboItemsSource}" SelectedItem="{Binding SelectedItem}"></ComboBox>

Simply inject the Datacontext from codebehind:

public MainWindow()
{
    InitializeComponent();
    DataContext = new ViewModel();
}

And build the view model:

public abstract class ViewModelBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
    {
        var propertyChanged = PropertyChanged;

        propertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}
public class ViewModel : ViewModelBase
{
    private string _selectedItem;
    public List<string> MyComboItemsSource { get; }

    public string SelectedItem
    {
        get { return _selectedItem; }
        set
        {
            _selectedItem = value;
            NotifyPropertyChanged();
        }
    }

    public ViewModel()
    {
        MyComboItemsSource = new List<string>();
        MyComboItemsSource.Add("hello");
        MyComboItemsSource.Add("world");

        SelectedItem = MyComboItemsSource.First();
    }
}

(Note the example implementation of INotifyPropertyChanged)

After that, it should be straightforward to build it in a datagrid if you want to.

Just make your step smaller.

Hope it helps.

Ouarzy
  • 3,015
  • 1
  • 16
  • 19