简体   繁体   中英

How to set value in WPF datagridtextcolumn cell when the datagridcombobox cell value is changed?

I am developing an application using WPF 4.0 Datagrid. My Datagrid grid has one datagridcomboboxcolumn and one datagridtextcolumn. How to change the datagrid text cell value using the datagridcomboboxcolumn's SelectedIndex_Changed event?

I recommend using the MVVM approach to creating WPF applications. In general, this means that you'll stop handling discrete events such as SelectedIndex_Changed and instead bind to observable objects in your ViewModel (VM) and/or Model (M).

With this architecture, solving your problem is easy. Simply bind your DataGridComboBoxColumn's SelectedItemBinding to a property on an object of your DataGrid's ItemSource. Then, bind your DataGridTextColumn to that property. This is better explained in code:

View:

<!-- Previous Window XAML omitted, but you must set it's DataContext to the ViewModel -->
<DataGrid
    CanUserAddRows="False"
    AutoGenerateColumns="False"
    ItemsSource="{Binding People}"
    >
    <DataGrid.Columns>
        <DataGridTextColumn
            Header="Selected Name"
            Binding="{Binding Name}"
            />
        <DataGridComboBoxColumn
            Header="Available Names"
            SelectedItemBinding="{Binding Name}"
            >
            <DataGridComboBoxColumn.ElementStyle>
                <Style TargetType="{x:Type ComboBox}">
                    <Setter Property="ItemsSource" Value="{Binding Names}" />
                </Style>
            </DataGridComboBoxColumn.ElementStyle>
            <DataGridComboBoxColumn.EditingElementStyle>
                <Style TargetType="{x:Type ComboBox}">
                    <Setter Property="ItemsSource" Value="{Binding Names}" />
                </Style>
            </DataGridComboBoxColumn.EditingElementStyle>
        </DataGridComboBoxColumn>
    </DataGrid.Columns>
</DataGrid>

ViewModel:

internal class MainWindowViewModel : ViewModelBase
{
    private ObservableCollection<Person> _people;
    public ObservableCollection<Person> People
    {
        get
        {
            _people = _people ?? new ObservableCollection<Person>()
            {
                new Person(),
                new Person(),
                new Person(),
            };

            return _people;
        }
    }
}

Model:

internal class Person : INotifyPropertyChanged
{
    private static ObservableCollection<string> _names = new ObservableCollection<string>()
    {
        "Chris",
        "Steve",
        "Pete",
    };

    public ObservableCollection<string> Names
    {
        get { return _names; }
    }

    private string _name;
    public string Name
    {
        get { return _name; }
        set
        {
            if (_name != value)
            {
                _name = value;
                this.RaisePropertyChanged(() => this.Name);
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged<T>(Expression<Func<T>> expr)
    {
        var memberExpr = expr.Body as MemberExpression;

        if (memberExpr != null)
        {
            var handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(memberExpr.Member.Name));
            }
        }
        else
        {
            throw new ArgumentException(String.Format("'{0}' is not a valid expression", expr));
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM