如何使用类继承时,有条件地插入定制的ListView列表项的复选框? [英] How to conditionally insert a Checkbox in a list item of a customized ListView when using class inheritance?

查看:148
本文介绍了如何使用类继承时,有条件地插入定制的ListView列表项的复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尽量让定制的ListView如果具体的继承类希望如此,填补了一些东西,最初的复选框每个列表项。当前未显示复选框,所以我想的东西ContentControl中我的code是莫名其妙的错误。

I try to make a customized ListView which fills each list item with some stuff and an initial Checkbox if the concrete inheriting class wishes so. Currently no Checkbox is displayed so I guess my code of the ContentControl stuff is somehow erroneous.

<UserControl x:Class="local:MyListView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="some/path/here">
<ListView>
<ListView.View>
    <GridView>
        <GridViewColumn>
            <GridViewColumn.CellTemplate>
                <DataTemplate>
                    <!-- Each list item: [Checkbox] Label -->
                    <StackPanel Orientation="Horizontal">
                        <!-- The code for the optional check box -->
                        <ContentControl>
                            <ContentControl.Style>
                                <Style TargetType="ContentControl">
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding IsCheckable, RelativeSource={RelativeSource AncestorType=local:MyListView}}" 
                                                     Value="True">
                                            <Setter Property="Template">
                                                <Setter.Value>
                                                    <ControlTemplate>
                                                        <CheckBox IsChecked="{Binding Path=SomeProperty}" />
                                                    </ControlTemplate>
                                                </Setter.Value>
                                            </Setter>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </ContentControl.Style>
                        </ContentControl>
                        <!-- The non-optional test label -->
                        <Label Content="Test Content" />
                    </StackPanel>
                </DataTemplate>
            </GridViewColumn.CellTemplate>
        </GridViewColumn>
    </ListView.View>
</ListView>
</UserControl>

在code背后:

The Code behind:

abstract partial class MyListView {
    protected MyListView () {
        InitializeComponent();
    }

    protected abstract bool IsCheckable { get; }
}

// A checkbox should be displayed - but it's not...
public class MyListView1 : MyListView {
    protected bool IsCheckable { get { return true; } }
}

public class MyListView2 : MyListView {
    protected bool IsCheckable { get { return false; } }
}

在检查输出窗口,我发现以下消息(我不知道如何利用):

When inspecting the output window I discovered following message (that I don't know how to make use of):

System.Windows.Data信息:10:使用绑定和没有有效的回退值存在无法检索值;使用默认值来代替。 BindingEx pression:路径= IsCheckable;的DataItem ='MyListView2'(名称='');目标元素是'ContentControl中'(名称=''); target属性是'NoTarget'(类型'对象')

System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=IsCheckable; DataItem='MyListView2' (Name=''); target element is 'ContentControl' (Name=''); target property is 'NoTarget' (type 'Object')

System.Windows.Data错误:39:BindingEx pression路径错误:'IsCheckable'属性不是'对象'''MyListView2'(名称='')'找到。 BindingEx pression:路径= IsCheckable;的DataItem ='MyListView2'(名称='');目标元素是'ContentControl中'(名称=''); target属性是'NoTarget'(类型'对象')

System.Windows.Data Error: 39 : BindingExpression path error: 'IsCheckable' property not found on 'object' ''MyListView2' (Name='')'. BindingExpression:Path=IsCheckable; DataItem='MyListView2' (Name=''); target element is 'ContentControl' (Name=''); target property is 'NoTarget' (type 'Object')

System.Windows.Data信息:19:BindingEx pression不能因为缺少信息检索值。 BindingEx pression:路径= IsCheckable;的DataItem ='MyListView2'(名称='');目标元素是'ContentControl中'(名称=''); target属性是'NoTarget'(类型'对象')

System.Windows.Data Information: 19 : BindingExpression cannot retrieve value due to missing information. BindingExpression:Path=IsCheckable; DataItem='MyListView2' (Name=''); target element is 'ContentControl' (Name=''); target property is 'NoTarget' (type 'Object')

System.Windows.Data信息:20:BindingEx pression无法检索空数据项值。被分离或绑定到没有任何价值可空类型时,结合时,这可能发生。 BindingEx pression:路径= IsCheckable;的DataItem ='MyListView2'(名称='');目标元素是'ContentControl中'(名称=''); target属性是'NoTarget'(类型'对象')

System.Windows.Data Information: 20 : BindingExpression cannot retrieve value from null data item. This could happen when binding is detached or when binding to a Nullable type that has no value. BindingExpression:Path=IsCheckable; DataItem='MyListView2' (Name=''); target element is 'ContentControl' (Name=''); target property is 'NoTarget' (type 'Object')

同样的错误信息出现MyListView1。
请注意,这个问题从<一个进化产生href=\"http://stackoverflow.com/questions/12202294/how-to-conditionally-insert-a-checkbox-in-a-list-item-of-a-customized-listview?answertab=active#tab-top\">an旧的帖子。

The same error messages appear for MyListView1. Note that this question arose from the evolution of an older post.

推荐答案

据该的 MSDN绑定来源概述:

属性使用作为约束源属性的绑定必须
  是你的类的公共属性。明确定义的接口
  属性不能结合的目的被访问,也不能保护
  具有没有碱专用内部或虚拟属性
  实施

The properties you use as binding source properties for a binding must be public properties of your class. Explicitly defined interface properties cannot be accessed for binding purposes, nor can protected, private, internal, or virtual properties that have no base implementation.

这篇关于如何使用类继承时,有条件地插入定制的ListView列表项的复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆