在 WPF 中的 DataGrid 列中添加两个项目

Add two items in DataGrid column in WPF

我想要 CheckBoxTextBlock 一起出现在 DataGrid 的列中,如下所示:

<DataGrid CanUserAddRows="False" CanUserDeleteRows="False" >
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="one">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock x:Name="textBlock1"></TextBlock>
                        <CheckBox x:Name="checkBox1"></CheckBox>
                    </StackPanel>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

我想将数据库中的数据绑定到此列: 我在数据库字段中的数据是这样的:"0-Hello""1-Bye"

如果字段为 "0-hello",则应取消选中 checkBox1,并且 textBlock1 应等于 "hello"

如果字段为 "1-bye",则应检查 checkBox1,并且 textBlock1 应等于 "hello"

我该怎么做?

使用 Datatrigger 设置复选框并使用 Converter 获取您不想查看的子字符串

<DataGrid  CanUserAddRows="False" CanUserDeleteRows="False" ItemsSource="{Binding Items}">
        <DataGrid.Columns>
            <DataGridTemplateColumn Header="one" >
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel >
                            <TextBlock x:Name="textBlock1" Text="{Binding YourProperty,Converter={StaticResource GetSubStringConverter}}"></TextBlock>
                            <CheckBox x:Name="checkBox1" >
                                <CheckBox.Style>
                                    <Style TargetType="CheckBox">
                                        <Setter Property="IsChecked" Value="True"/>
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding YourProperty[0]}" Value="0">
                                                <Setter Property="IsChecked" Value="False"></Setter>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </CheckBox.Style>
                            </CheckBox>
                        </StackPanel>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

转换器将简单地提取要显示的文本

 public class GetSubStringConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null) return null;
        return value.ToString().Substring(2);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

您所要做的就是为您的数据创建一个值转换器:

public class StringBooleanConverter : IValueConverter
    {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                    if(value.ToString() == "0-Hello")
                       return false;
                    else if(value.ToString() == "1-Bye")
                        return true;
                    return false;
            }

            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                    if(value is bool)
                    {
                            if((bool)value == true)
                                    return "1-Bye";
                            else
                                    return "0-Hello";
                    }
                    return "no";
            }
    }

并在您的 XAML 中调用它:

<DataGrid CanUserAddRows="False" CanUserDeleteRows="False" >
     <DataGrid.Resources>
         <local:StringBooleanConverter x:Key="StringBooleanConverter" />
     </DataGrid.Resources>
        <DataGrid.Columns>
            <DataGridTemplateColumn Header="one">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <TextBlock x:Name="textBlock1"></TextBlock>
                            <CheckBox x:Name="checkBox1" IsCheked="{Binding Path=YourBooleanProperty, Converter={StaticResource StringBooleanConverter}}"></CheckBox>
                        </StackPanel>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

注意:不要忘记在 Window 中标记本地名称空间。

<Window x:Class="WpfTutorialSamples.DataBinding.ConverterSample"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:YourConveterNameSpace"
        >
    //...
</Window> 

您可以使用自定义 IValueConverter 将字符串解析为您想要的文本或布尔值:

<DataGrid CanUserAddRows="False" CanUserDeleteRows="False" ItemsSource="{Binding Data}">
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="one">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock x:Name="textBlock1" Text="{Binding DataString, Converter={StaticResource DataToTextConverter}}"  />
                        <CheckBox x:Name="checkBox1" Text="{Binding DataString, Converter={StaticResource DataToBooleanConverter}}"/>
                    </StackPanel>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

在您的资源中:

<UserControl.Resources>
    <local:DataToTextConverter x:Key="DataToTextConverter"/>
    <local:DataToBooleanConverter x:Key="DataToBooleanConverter"/>
</UserControl.Resources>

后面的代码:

[ValueConversion(typeof(String), typeof(String))]
public class NotificationSeverityColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        //strip the first two characters
        return parsedString;
    }
}