单击时,是否可以使DataGrid扩展新行? [英] Is there a way to make a DataGrid expand a new row, when clicking on it?

查看:86
本文介绍了单击时,是否可以使DataGrid扩展新行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去的几个月中,我们一直在开发新的WPF应用程序,以替换旧的VB6应用程序(谢天谢地!!),我们使用DataGrid以及DataGridComboxBoxColumn来指定下拉菜单.用户选择.在我们的测试中,我们发现 您必须从下拉列表中选择一些内容,然后在数据网格中的其余行之间进行制表(或至少转到数据网格的最后一列并从中进行制表),如果要创建新的行就可以使用它.昨天我们发布了 这款新应用,几乎每个人都广受好评.

We've just spent the last several months working on a new WPF application, to replace an old VB6 app (thank God that's gone!) We use the DataGrid, along with the DataGridComboxBoxColumn to specify a dropdown for the user to select. In our testing we've found that you have to select something from the dropdown, and then tab through the rest of the row in the data grid (or at least go to the last column in the data grid and tab out of it), if you want to create a new row to be able to use it. Yesterday we released this new app, and it has been well received by nearly everyone.

但是,有一个用户没有浏览该行.相反,他将从下拉列表中选择某项,然后感到沮丧,因为数据网格没有给他另一行,因此他可以在另一行中选择其他内容(这是之前的 他已经完成了第一行的填写).那么,WPF DataGrid控件是否可以执行此特定用户想要执行的操作?我可以设置一些属性吗?

However there is one user who doesn't tab through the row. Instead he'll select something from the dropdown and then get frustrated because the data grid doesn't give him another row so he can select something different in that other row (this is before he's finished filling out the first row). So, is there a way with the WPF DataGrid control to do what this particular user is trying to do? Some property I can set?

Rod

推荐答案

根据您的描述,如果我没有误会,您想为下一行添加自动填充:

Based on your description, if I'm not misunderstanding, you want to implement automatic filling for the next new row:

如果是,请检查以下代码:

If so, please check the following code:

XAML,我在最后一列中使用了SelectionChanged事件:

XAML, I used the SelectionChanged event for the last column:

<DataGrid Margin="5" ItemsSource="{Binding}" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
                <DataGridTextColumn Header="Capital" Binding="{Binding Capital}"/>
                <DataGridTextColumn Header="Time Zone" Binding="{Binding TimeZone}"/>
                <DataGridTemplateColumn Header="Cities" Width="*">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox Margin="2" ItemsSource="{Binding Cities}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="TestColumn" Width="*">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox Margin="2" ItemsSource="{Binding TestColumn}" SelectionChanged="ComboBox_SelectionChanged" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

代码背后:

Class MainWindow

    Private states As New ObservableCollection(Of State)()

    Public Sub New()
        InitializeComponent()


        states.Add(New State() With { _
                   .Name = "Maryland", _
                   .Capital = "Annapolis", _
                   .TimeZone = "Eastern", _
                   .Cities = New ObservableCollection(Of String)() From { _
                       "Frederick", _
                       "Baltimore", _
                       "Rockville" _
                   }, _
                   .TestColumn = New ObservableCollection(Of String)() From { _
                       "1", _
                       "2", _
                       "3" _
                   } _
               })

        DataContext = states
    End Sub


    Private Sub ComboBox_SelectionChanged(sender As Object, e As SelectionChangedEventArgs)
            'Here you can modify items as you like:)
            states.Add(New State() With { _
            .Cities = New ObservableCollection(Of String)() From { _
                "Los Angeles", _
                "San Fransisco", _
                "San Diego" _
            }, _
            .TestColumn = New ObservableCollection(Of String)() From { _
                "1", _
                "2", _
                "3" _
            } _
        })
    End Sub
End Class

Public Class State
    Public Property Name() As String
        Get
            Return m_Name
        End Get
        Set(value As String)
            m_Name = Value
        End Set
    End Property
    Private m_Name As String

    Public Property TimeZone() As String
        Get
            Return m_TimeZone
        End Get
        Set(value As String)
            m_TimeZone = Value
        End Set
    End Property
    Private m_TimeZone As String

    Public Property Capital() As String
        Get
            Return m_Capital
        End Get
        Set(value As String)
            m_Capital = Value
        End Set
    End Property
    Private m_Capital As String

    Public Property Cities() As ObservableCollection(Of String)
        Get
            Return m_Cities
        End Get
        Set(value As ObservableCollection(Of String))
            m_Cities = Value
        End Set
    End Property
    Private m_Cities As ObservableCollection(Of String)

    Public Property TestColumn() As ObservableCollection(Of String)
        Get
            Return m_TestColumn
        End Get
        Set(value As ObservableCollection(Of String))
            m_TestColumn = Value
        End Set
    End Property
    Private m_TestColumn As ObservableCollection(Of String)
End Class


这篇关于单击时,是否可以使DataGrid扩展新行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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