我想基于另一列的值在一个列中创建具有动态单元格控制的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方法和代码隐藏方法)是首选。其他可能的解决方法也表示赞赏。
答案 0 :(得分:1)
您可以将ContentControl
与Style
结合使用,以根据Content
源属性的值来设置ControlType
属性,例如:
<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>