带组合框的Wpf数据网格 [英] Wpf datagrid with combobox

查看:77
本文介绍了带组合框的Wpf数据网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,DataGrid绑定到ObservableCollection,其中每列是一个ComboBox。第一个ComboBox的ItemsSource绑定到ObjectDataProvider。其他ComboBox使用项目填充,具体取决于第一个ComboBox中的SelectedItem。我正在使用DataGridTemplateColumn \ DataTemplate \ ComboBox ...问题是,当我从第一个ComboBox中选择一个项目时,未设置SelectedItem,因为DataGrid绑定的集合中不存在任何项目。一旦我开始从第一个ComboBox中选择一个项目,DataGrid就应该创建新行(将对象添加到集合中)。我无法添加新行。



I have a issue with DataGrid bound to ObservableCollection, where each column is a ComboBox. First ComboBox has ItemsSource binded to ObjectDataProvider. The other ComboBoxes are populated with items depending on the SelectedItem in first ComboBox. I'm using DataGridTemplateColumn\DataTemplate\ComboBox... The problem is that when I select an item from first ComboBox, SelectedItem is not set, because no items exist in the collection the DataGrid is bound to. DataGrid should create new row (add object to collection) as soon as I start selecting an item from first ComboBox. I can't add new row.

<DataGrid DockPanel.Dock="Top" ItemsSource="{Binding Path=Routes, Mode=TwoWay}"                  
                    AutoGenerateColumns="False"                                            
                    CanUserDeleteRows="True"
                    CanUserAddRows="True"    
                    IsSynchronizedWithCurrentItem="True"                    
                    GridLinesVisibility="All" 
                  >
            <DataGrid.Resources>                
                <ObjectDataProvider x:Key="Modules" ObjectType="{x:Type serv:PortPinDataService}" MethodName="GetModules"/>
            </DataGrid.Resources>
            <DataGrid.Columns>

                <DataGridTemplateColumn Header="Module" Width="*">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox ItemsSource="{Binding Source={StaticResource Modules}}"
                                      DisplayMemberPath="Name"
                                      SelectedItem="{Binding Path=CurrentModule, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">                                
                            </ComboBox>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>                    
                </DataGridTemplateColumn>
                
                <DataGridComboBoxColumn Header="Module" ItemsSource="{Binding Source={StaticResource Modules}}"
                                        SelectedItemBinding="{Binding Path=CurrentModule, UpdateSourceTrigger=PropertyChanged}"
                                        Width="80"    
                                        DisplayMemberPath="Name"
                                        >
                    <DataGridComboBoxColumn.ElementStyle>
                        <Style TargetType="ComboBox">                            
                            <Setter Property="Width" Value="80"/>
                        </Style>
                    </DataGridComboBoxColumn.ElementStyle>
                </DataGridComboBoxColumn>                

                <DataGridTemplateColumn Header="Signal" Width="*">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox ItemsSource="{Binding Path=SignalsByModule}" DisplayMemberPath="SignalName" SelectedItem="{Binding Path=CurrentSignalRegister, UpdateSourceTrigger=PropertyChanged}"></ComboBox>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

                <DataGridTemplateColumn Header="Route to" Width="*">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox ItemsSource="{Binding Path=Registers}" DisplayMemberPath="RegRouteInfo"></ComboBox>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

            </DataGrid.Columns>
        </DataGrid>





所以我的问题是,如果使用DataGridTemplateColumn,为什么没有将新对象添加到DataGrid集合?



我尝试了什么:



如果我更改了第一列在DataGridComboBoxColumn中,一旦我开始选择一个项目并且设置了SelectedItem,就会将一个新项目添加到集合中。



So my question is why a new object is not added to DataGrid collection if using DataGridTemplateColumn?

What I have tried:

If I change the first column to DataGridComboBoxColumn a new item is added to collection as soon as I start picking an item and SelectedItem is set.

推荐答案

DataGrid绑定示例中的Windows Easy ComboBox C#for Visual Studio 2013中的示例 [ ^ ]


这篇关于带组合框的Wpf数据网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆