当 datagridcombobox 单元格值更改时,如何在 WPF datagridtextcolumn 单元格中设置值?

发布于 2024-10-13 02:40:27 字数 167 浏览 4 评论 0原文

我正在使用 WPF 4.0 Datagrid 开发一个应用程序。我的 Datagrid 网格有一个 datagridcombobox 列和一个 datagridtext 列。如何使用 datagridcomboboxcolumn 的 SelectedIndex_Changed 事件更改 datagrid 文本单元格值?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你在看孤独的风景 2024-10-20 02:40:27

我建议使用 MVVM 方法来创建 WPF 应用程序。一般来说,这意味着您将停止处理离散事件,例如 SelectedIndex_Changed,而是绑定到 ViewModel (VM) 和/或 Model (M) 中的可观察对象。

有了这个架构,解决你的问题就很容易了。只需将 DataGridComboBoxColumn 的 SelectedItemBinding 绑定到 DataGrid ItemSource 对象上的属性即可。然后,将 DataGridTextColumn 绑定到该属性。这在代码中得到了更好的解释:

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));
        }
    }
}

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));
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文