如何在使用类继承时有条件地在自定义ListView的列表项中插入复选框?

时间:2012-09-13 17:15:01

标签: wpf listview binding controltemplate contentcontrol

我尝试制作一个自定义的ListView,如果具体的继承类希望如此填充每个列表项的一些东西和一个初始的Checkbox。目前没有显示Checkbox,所以我想我的ContentControl内容的代码有点错误。

<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>

守则背后:

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; } }
}

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

  

System.Windows.Data信息:10:无法使用绑定检索值,并且不存在有效的回退值;使用默认值。 BindingExpression:路径= IsCheckable; DataItem ='MyListView2'(Name =''); target元素是'ContentControl'(Name =''); target属性是'NoTarget'(类型'Object')

     

System.Windows.Data错误:39:BindingExpression路径错误:'object'''MyListView2'(Name ='')'上找不到'IsCheckable'属性。 BindingExpression:路径= IsCheckable; DataItem ='MyListView2'(Name =''); target元素是'ContentControl'(Name =''); target属性是'NoTarget'(类型'Object')

     

System.Windows.Data信息:19:由于缺少信息,BindingExpression无法检索值。 BindingExpression:路径= IsCheckable; DataItem ='MyListView2'(Name =''); target元素是'ContentControl'(Name =''); target属性是'NoTarget'(类型'Object')

     

System.Windows.Data信息:20:BindingExpression无法从null数据项中检索值。当分离绑定或绑定到没有值的Nullable类型时,可能会发生这种情况。 BindingExpression:路径= IsCheckable; DataItem ='MyListView2'(Name =''); target元素是'ContentControl'(Name =''); target属性是'NoTarget'(类型'Object')

MyListView1会显示相同的错误消息。 请注意,这个问题源于an older post的演变。

1 个答案:

答案 0 :(得分:0)

根据msdn Binding Sources Overview:

  

您用作绑定的绑定源属性的属性必须   是你班级的公共财产。明确定义的接口   无法访问属性用于绑定目的,也不能保护,   没有基础的私有,内部或虚拟属性   实施