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

查看:19
本文介绍了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 的集合,它会指定名称、输入类型和帮助信息(只是文本),然后使用binding 将集合绑定到 DataGrid.基本上,这些将充当 ViewModel,并且在 DataGrid 中设置值将通过 ViewModel 向上流动到实际模型(就像 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).

但是,当我查看可用的文档时,我找不到任何讨论像这样动态更改列类型的方法的地方.我现在倾向于使用网格代替,并手动布置输入(仍然使用绑定,但单独绑定每个元素).不过,这对我来说需要更多的手动工作,所以我想知道是否有一种相对简单的方法来实现我的第一个想法.似乎我应该可以用 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.

推荐答案

您可以使用模板列来做到这一点.

You can do that using a template column.

这个将根据行中的数据显示一个或两个文本框.

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天全站免登陆