WPF DataGrid different edit controls within a single column

后端 未结 2 1665
灰色年华
灰色年华 2021-01-01 04:20

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:

| N         


        
相关标签:
2条回答
  • 2021-01-01 04:31

    You can do that using a template column.

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

    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>
    
    0 讨论(0)
  • 2021-01-01 04:56

    I had to do this once in an ASP.NET application with a GridView, and though it's a different technology I think the answer will be the same. What I had to do was simply add ALL of the different kinds of controls in each cell, but initially leave them not visible.

    Then you can override whatever event is fired after each item is bound to the row in the grid (I'm new to WPF as well, I'm not sure what the events are), change the visibility of the appropriate control in the cell based on whatever information you know in the object you're binding for that row, and then populate or bind the control to the appropriate value.

    It does get to be more fun when you need to get the values back out, but I think I ended up search for the control that was visible and then making a decision based on the control's type. I think this is about the only way you're going to be able to accomplish it, since every grid control I've used from Microsoft seems to assume that your columns will never need different types of controls.

    I've asked nearly the same question before on SO. Here's the post. Maybe it will get the ball rolling for you.

    0 讨论(0)
提交回复
热议问题