WPF DataGrid ComboBox 列:在整个列中传播标题组合框

发布于 2024-07-28 22:41:52 字数 1843 浏览 3 评论 0原文

嘿! 这是我的问题:

我在 WPF 中有一个 Datagrid,并且第一列是 DataGridComboBoxColumn。

我想做的是为该列添加一个标题,并带有一个组合框:通过在整个列中传播来更改标题。

我可以直观地完成此操作,但是当我提交数据时,与数据网格绑定的列表不包含新的组合框值。

<dg:DataGridComboBoxColumn SelectedItemBinding="{Binding BIBStatus}"                                            
                           ItemsSource="{Binding Source={StaticResource typeStatus}}" 
                           Width="0.20*">
                    <dg:DataGridComboBoxColumn.HeaderTemplate>
                        <DataTemplate>
                            <StackPanel>
                                <TextBlock Text="Built-In Bridge"/>
                                <ComboBox SelectedItem="{Binding BIBStatus}" 
                                          SelectionChanged="ComboBox_SelectionChanged"
                                          ItemsSource="{Binding Source={StaticResource typeStatus}}"/>
                            </StackPanel>                                
                        </DataTemplate>
                    </dg:DataGridComboBoxColumn.HeaderTemplate>
                </dg:DataGridComboBoxColumn>

在 ComboBox_SelectionChanged 中,我有以下代码:

DataGridColumn comboCol = this.gridResults.Columns[0];

        for (int i = 0; i < this.gridResults.Items.Count; i++)
        {
            ComboBox myCmBox = (comboCol.GetCellContent(this.gridResults.Items[i]) as ComboBox);

            myCmBox.SelectedValue = ((ComboBox)sender).SelectedValue;
        }

当我提交数据时,我将 DataContext 列表提交到 Datagrid; 如果我一次更改寻址一行的第一列的值,即单击每行中带有组合框的单元格,则这些值将传播到 DataContext 中的列表,但是如果我通过标题更改第一列的值它不是。

谁能告诉我我错过了什么? 我猜这是我影响每一行的方式,但我尝试过 SelectedValue、SelectedItem 和 SelectedIndex ... 都无济于事。

提前致谢...

Hey there! Here's my question:

I've got a Datagrid in WPF and I have a first column that is a DataGridComboBoxColumn.

What I'd like to do is to have a header for that column also with a combobox: altering the header with propagate throughout the column.

I can get this done visually, but when I submit the data, the list that is bound with the Datagrid does not carry the new combobox values.

<dg:DataGridComboBoxColumn SelectedItemBinding="{Binding BIBStatus}"                                            
                           ItemsSource="{Binding Source={StaticResource typeStatus}}" 
                           Width="0.20*">
                    <dg:DataGridComboBoxColumn.HeaderTemplate>
                        <DataTemplate>
                            <StackPanel>
                                <TextBlock Text="Built-In Bridge"/>
                                <ComboBox SelectedItem="{Binding BIBStatus}" 
                                          SelectionChanged="ComboBox_SelectionChanged"
                                          ItemsSource="{Binding Source={StaticResource typeStatus}}"/>
                            </StackPanel>                                
                        </DataTemplate>
                    </dg:DataGridComboBoxColumn.HeaderTemplate>
                </dg:DataGridComboBoxColumn>

In the ComboBox_SelectionChanged I have the following code:

DataGridColumn comboCol = this.gridResults.Columns[0];

        for (int i = 0; i < this.gridResults.Items.Count; i++)
        {
            ComboBox myCmBox = (comboCol.GetCellContent(this.gridResults.Items[i]) as ComboBox);

            myCmBox.SelectedValue = ((ComboBox)sender).SelectedValue;
        }

When I submit the data, I submit the list that is DataContext to the Datagrid; if I change the value of the first column addressing a row at a time, i.e. clicking the cell with the combobox in each row, the values are propagated to the list in DataContext, however if I change the value of the first column by the header it does not.

Can anyone tell me what I'm missing? I'm guessing it's the way I affect each row, but I've tried SelectedValue, SelectedItem and SelectedIndex... all to no avail.

Thanks in advance...

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

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

发布评论

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

评论(1

﹎☆浅夏丿初晴 2024-08-04 22:41:52

我想我可能已经解决了它......或者至少掩盖了它......

DataGridColumn comboCol = this.gridResults.Columns[0];

        for (int i = 0; i < this.gridResults.Items.Count; i++)
        {
            ComboBox myCmBox = (comboCol.GetCellContent(this.gridResults.Items[i]) as ComboBox);
            myCmBox.SelectedItem = ((ComboBox)sender).SelectedItem;
        }

        if (this._results != null)
        {
            foreach (Device device in _results)
            {
                device.BIBStatus = (TypeStatus)Enum.ToObject(typeof(TypeStatus), ((ComboBox)sender).SelectedValue);
            }
        }

我尝试仅更改数据上下文并希望双向绑定能够工作,但只有当我专注于每行的组合框时它才会发生。 所以我想:“为什么不双向呢?”
正如您所看到的,我更改了每个组合框的所选项目和每个设备的 BIB 状态(数据上下文部分)。 这样我就得到了想要的效果。

然而,我不认为这个解决方案是最好的,我仍然希望有一种方法可以做到这一点,而不是成为一个恶棍。

有任何建议仍然欢迎!

I think I may have solved it... or at least disguised it...

DataGridColumn comboCol = this.gridResults.Columns[0];

        for (int i = 0; i < this.gridResults.Items.Count; i++)
        {
            ComboBox myCmBox = (comboCol.GetCellContent(this.gridResults.Items[i]) as ComboBox);
            myCmBox.SelectedItem = ((ComboBox)sender).SelectedItem;
        }

        if (this._results != null)
        {
            foreach (Device device in _results)
            {
                device.BIBStatus = (TypeStatus)Enum.ToObject(typeof(TypeStatus), ((ComboBox)sender).SelectedValue);
            }
        }

I tried to change only the datacontext and hope for the two-way binding to work, but it only did when I focused on each row's combobox. So I thought: "why not both ways?"
As you can see, I change both each combobox's selecteditem and each device's BIB status (the datacontext part). This way I get the desired effect.

However I do not think this solution is the best one and I'm still hoping there is a way I can do this without being a scoundrel.

Any suggestions are still welcome!

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