WPF DataGrid中一列中的动态单元格控件
Posted
技术标签:
【中文标题】WPF DataGrid中一列中的动态单元格控件【英文标题】:Dynamic cell control in one column in WPF DataGrid 【发布时间】:2019-02-24 22:41:40 【问题描述】:我想根据另一列中的值在一个列中创建具有动态单元格控件的 WPF DataGrid,如下图所示。
XAML
<Window.Resources>
<Style x:Key="DataGridCellStyle1" TargetType="x:Type DataGridCell">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="x:Type DataGridCell">
<Grid Height="21.96">
<ComboBox x:Name="cbCondition1">
<ComboBoxItem Content="1"/>
<ComboBoxItem Content="2"/>
</ComboBox>
<TextBox x:Name="tbCondition2" Text="text"/>
</Grid>
<ControlTemplate.Triggers>
<DataTrigger Binding="Binding TypeColumn" Value="ComboBox">
<Setter Value="Visible" TargetName="cbCondition1" Property="Visibility"/>
<Setter Value="Hidden" TargetName="tbCondition2" Property="Visibility"/>
</DataTrigger>
<DataTrigger Binding="Binding TypeColumn" Value="TextBox">
<Setter Value="Hidden" TargetName="cbCondition1" Property="Visibility"/>
<Setter Value="Visible" TargetName="tbCondition2" Property="Visibility"/>
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="DynamicResource x:Static SystemColors.HighlightBrushKey"/>
<Setter Property="Foreground" Value="DynamicResource x:Static SystemColors.HighlightTextBrushKey"/>
<Setter Property="BorderBrush" Value="DynamicResource x:Static SystemColors.HighlightBrushKey"/>
</Trigger>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="BorderBrush" Value="DynamicResource x:Static DataGrid.FocusBorderBrushKey"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<DataGrid HorizontalAlignment="Left" Width="500" Grid.ColumnSpan="2" VerticalAlignment="Top" x:Name="GridWorkers"
ItemsSource="Binding Workers" Grid.Row="1" SelectedItem="Binding SelectedWorker" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Control Type" Binding="Binding TypeColumn" Width="90" IsReadOnly="True"/>
<DataGridTemplateColumn Header="Dynamic Control" CellStyle="StaticResource DataGridCellStyle1"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
我可以使用 xaml 样式来实现这一点,但最好寻找编程方式在 DataGrid 单元格中动态添加控件,即从 C# 代码(mvvm 方法和代码隐藏方法)。其他可能的解决方案也值得赞赏。
【问题讨论】:
这个问题不完整,如何创建一个最小、完整和可验证的例子:***.com/help/mcve 【参考方案1】:您可以将ContentControl
与Style
结合使用,根据ControlType
源属性的值设置Content
属性,例如:
<DataGrid ... AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Control Type" Binding="Binding ControlType" />
<DataGridTemplateColumn Header="Dynamic Control">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ContentControl>
<ContentControl.Style>
<Style TargetType="ContentControl">
<Style.Triggers>
<DataTrigger Binding="Binding ControlType" Value="ComboBox">
<Setter Property="Content">
<Setter.Value>
<ComboBox />
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger Binding="Binding ControlType" Value="TextBox">
<Setter Property="Content">
<Setter.Value>
<TextBox Text="text...." />
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
【讨论】:
以上是关于WPF DataGrid中一列中的动态单元格控件的主要内容,如果未能解决你的问题,请参考以下文章
EasyUi DataGrid中数据编辑方式及编辑后数据获取,校验处理
WPF DataGrid在同一列中的不同控件 - 不正确的绑定