简体   繁体   English

值为null或不为null时,如何在列表视图中选中复选框?

[英]How do I check a checkbox in a listview when the value is null or not null?

I am creating a WPF ListView with multiple GridViewColumn s and would like to set a CheckBox IsChecked property to true or false based on a binding and whether or not that data value is null or not null. 我正在创建具有多个GridViewColumn的WPF ListView ,并希望根据绑定以及该数据值是否为null来将CheckBox IsChecked属性设置为true或false。

I tried using a DataTrigger below to accomplish this but it doesn't seem to be working. 我尝试使用下面的DataTrigger来完成此操作,但它似乎没有用。 It basically keeps everything unchecked in the ListView . 它基本上使所有内容在ListView保持未选中状态。

<GridViewColumn Header="W()" Width="20" >
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <CheckBox Content="Tell me something" >
                <CheckBox.Style>
                    <Style TargetType="{x:Type CheckBox}">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding WebsiteJob}" Value="{x:Null}">
                                <Setter Property="IsChecked" Value="False" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </CheckBox.Style>
            </CheckBox>
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>

Is there a way for me to handle specific columns in a ListView with GridViewColumn based on their binding? 有没有办法让我GridViewColumn基于GridViewColumn的绑定处理ListView特定列? Truly in this situation I want to supply the CheckBox with some custom content as well as set it to IsChecked. 确实在这种情况下,我想为CheckBox提供一些自定义内容并将其设置为IsChecked。 I am pretty new to WPF so be easy. 我对WPF还是很陌生,所以很容易。

Ok here is what I did based on the advice in the question comments. 好的,这是我根据问题评论中的建议执行的操作。 I created an IValueConverter which stores the original object. 我创建了一个存储原始对象的IValueConverter。 This allows me to perform all kinds of validation on the nested object itself and then set it back when its done. 这使我可以对嵌套对象本身执行各种验证,然后在完成时将其重新设置。

public class CheckboxIsCheckedValueConverter : IValueConverter {

    public object OriginalValue;

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
        if (value != null) {
            OriginalValue = value;
            return true;
        }
        return false;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
        if ((bool)value == true) {
            return OriginalValue;
        } else {
            return null;
        }
    }
}

Also, I had to change the xaml import to the following below. 另外,我必须将xaml导入更改为以下内容。 Even though x:Shared doesn't popup in Intellisense it still does exist. 即使x:Shared在Intellisense中没有弹出,它仍然存在。 This makes sure that each instance of the converter is unique and allows me to store the original value. 这样可以确保转换器的每个实例都是唯一的,并允许我存储原始值。

<l:CheckboxIsCheckedValueConverter x:Key="CheckboxIsCheckedValueConverter" x:Shared="False" />

Modified GridViewColumn 修改后的GridViewColumn

<GridViewColumn HeaderContainerStyle="{StaticResource SmokeStyleHeaders}" Header="W()" Width="30" >
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <CheckBox Foreground="White" Checked="CheckBox_Checked" IsChecked="{Binding WebsiteJob, Converter={StaticResource CheckboxIsCheckedValueConverter}}" Content="{Binding WebsiteJob, Converter={StaticResource WebsiteJobValueConverter}}" />
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM