我想在CheckBox
列中合并TextBlock
和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"
。
我该怎么做?
答案 0 :(得分:0)
使用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();
}
}
答案 1 :(得分:0)
您所要做的就是为您的数据创建一个Value转换器:
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>
注意:不要在窗口中使用decalre本地命名空间。
<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>
答案 2 :(得分:0)
您可以使用自定义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;
}
}