我设法创建了一个在同一列上包含不同类型控件的数据网格。但是,我无法在这些控件中获得用户输入。
项目
public class Item
{
/// <summary>
/// item name
/// </summary>
public string ParameterName { get; set; }
/// <summary>
/// Control type
/// </summary>
public ControlType ControlType { get; set; }
/// <summary>
/// New item value
/// </summary>
public object ParameterValue { get; set; }
/// <summary>
/// Current item value
/// </summary>
public string CurrentParameterValue { get; set; }
}
ControlType
public enum ControlType
{
TextBox,
ComboBox,
CheckBox,
TextBlock
}
数据网格
<DataGrid x:Name="dgridGeneral" ItemsSource="{Binding}" AutoGenerateColumns="False" CanUserAddRows="False" CanUserResizeColumns="False" SelectionUnit="FullRow">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Name" Width="200">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding ParameterName}" Style="{StaticResource TextHeader}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="New Value" Width="200">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ContentControl Content="{Binding}">
<ContentControl.Style>
<Style TargetType="ContentControl">
<Style.Triggers>
<DataTrigger Binding="{Binding ControlType}" Value="CheckBox">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<CheckBox IsChecked="{Binding ParameterValue, Mode=TwoWay}"/>
</DataTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger Binding="{Binding ControlType}" Value="ComboBox">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<ComboBox ItemsSource="{Binding ComboBoxControlItemSource}" SelectedIndex="{Binding ParameterValue, Mode=TwoWay}" />
</DataTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger Binding="{Binding ControlType}" Value="TextBlock">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock Text="{Binding ParameterValue}"/>
</DataTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger Binding="{Binding ControlType}" Value="TextBox">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<TextBox Text="{Binding ParameterValue, Mode=TwoWay}" />
</DataTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Current Value" Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding CurrentParameterValue}" Style="{StaticResource TextBodyNormal}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
我还尝试在Item类中为不同的控件类型添加其他属性。例如,TextBox的“Text”属性,CheckBox的“IsCheck”属性,ComboBox的SelectedIndex属性。但是,我仍然无法获得用户输入。
有关如何获取用户输入的任何想法? 感谢
答案 0 :(得分:1)
如果您将绑定的ParameterValue
属性设置为Item
,则会设置UpdateSourceTrigger
的{{1}}属性:
PropertyChanged