WPF DataGrid在单列中编辑不同的控件 [英] WPF DataGrid different edit controls within a single column

查看:151
本文介绍了WPF DataGrid在单列中编辑不同的控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个WPF 4.0应用程序,其中我需要使用包含文本框或下拉列的列的网格,这取决于行。示例:

I am developing a WPF 4.0 application where I need to make a grid that contains a column with either textbox or a dropdown depending on the row. Example:

| Name      |  Value                   | Help                                   |
| PROP1A    | [textbox]                | Description of prop1a                  |
| Prop2A    | [dropdown v]             | Description of prop2a                  |
| Prop3A    | [textbox] [x checkbox]   | Description of prop2a                  |
| Prop4A    | [dropdown v]             | Description of prop2a                  |
| etc...

这个想法是用户有一个需要输入的值表,并且我们显示每个值旁边的名称和描述。一些值是需要使用文本框输入的数字,而其他的是一个文本框和一个复选框,还有一些是一个下拉列表。

The idea is that the user has a table of values that they need to input, and we display the name and description for each value alongside. Some of the values are numbers that need to be input with a textbox, while others are a textbox plus a checkbox, and still others are a dropdown.

我的初步想法是实现这一点基本上是一个我将称之为 RowDescriptor 的集合,它将指定名称,输入类型和帮助信息(仅仅是文本),然后使用绑定将集合绑定到DataGrid。基本上,这些将作为ViewModels,并且设置DataGrid中的值将通过ViewModel流向实际的Model(就像在MVVM应用程序的典型情况下)。

My initial thought was to implements this as basically a collection of what I'll call RowDescriptors that would specify the name, input type and help info (which is just text), and then use binding to bind the collection to the DataGrid. Basically, these would act as ViewModels, and setting the value in the DataGrid would flow up through the ViewModel to the actual Model (just like the in the typical case for an MVVM app).

当我查看我可用的文档时,我找不到任何地方讨论了如何动态地更改列的类型。我现在倾向于使用网格,并手动布局输入(仍然使用Binding,但单独绑定每个元素)。这将是更多的手动努力在我的方面,所以我想知道是否有一个相对简单的方式来实现我的第一个想法。看起来我应该能够使用DataGridTemplateColumn做一些事情,但是我对WPF来说是比较新的,我不知道我会如何做到这一点。

As I looked through the documentation I have available, though, I couldn't find anywhere that discussed a way of changing the type of the column dynamically like this. I am now leaning toward using a Grid instead, and manually laying out the inputs (still using Binding, but binding each element individually). This will be a lot more manual effort on my part though, so I wanted to find out if there was a relatively-straightforward way of implementing my first idea. It seems like I should be able to do something with DataGridTemplateColumn, but I'm relatively new to WPF and I'm not sure exactly how I'd go about doing this.

推荐答案

您可以使用模板列。

根据数据,这个文本框将显示一个或两个该行。

This one will show one or two text boxes depending on the data in the row.

CellTemplate正常显示,但是当编辑行时,它将被CellEditingTemplate替换。

The CellTemplate is shown normally, but it is replaced with CellEditingTemplate when the row is edited.

<DataGridTemplateColumn Header="Value" Width="350">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock 
            Text="{Binding EffectiveValue,Mode=OneWay,ValidatesOnDataErrors=True}" 
            ToolTip="{Binding EffectiveValue,Mode=OneWay}"
            TextWrapping="Wrap"
            />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>

    <DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>


                <TextBlock  Text="Value" Margin="4"/>
                <TextBox 
                Grid.Column="1"
                Text="{Binding ConfigurationValue,ValidatesOnDataErrors=True}" 
                ToolTip="{Binding ConfigurationValue}"
                TextWrapping="Wrap"
                AcceptsReturn="{Binding DataType, Mode=OneWay, 
                                    Converter={StaticResource ResourceKey=StringMatchBooleanConverter}, ConverterParameter=String}"
                gui:FocusAttacher.Focus="True" 
                />

                <TextBlock  Text="Default Value" Grid.Row="1" Margin="4"
                Visibility="{Binding DefaultConfigurationValue, Converter={StaticResource ResourceKey=NullVisibilityConverter}}"
                        />
                <TextBox 
                Grid.Column="1" 
                Grid.Row="1"
                Text="{Binding DefaultConfigurationValue, Mode=OneWay }" 
                ToolTip="{Binding DefaultConfigurationValue, Mode=OneWay}"
                TextWrapping="Wrap"
                IsReadOnly="True" 
                Visibility="{Binding DefaultConfigurationValue, Converter={StaticResource ResourceKey=NullVisibilityConverter}}"

                />

            </Grid>
        </DataTemplate>
    </DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>

这篇关于WPF DataGrid在单列中编辑不同的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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