ListView模板中的ListView不允许我绑定到父项ItemsSource

时间:2017-05-25 19:40:45

标签: c# wpf xaml listview templatebinding

所以我正在尝试构建一个很酷的ListView,它有一个" CheckAll"允许我检查列表视图中的所有项目的按钮。然后我可以在几个地方重用这个控件。所以我有一个ControlTemplate,我试图将一个新的ListView的ItemsSource属性绑定到父控件ItemsSource属性。如果我设置TargetType =" ListView"它让我定义它。但后来我得到了一个XML Parse Exception,它无法使用它。

<ListView x:Class="CheckAllBox.CheckAllListView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300"
         BorderThickness=".5" BorderBrush="Gray">
    <ListView.Resources>

    </ListView.Resources>
    <ListView.Template>
        <ControlTemplate TargetType="ListView">
            <Border BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <Border Margin="0" Padding="2" Background="White">
                        <CheckBox Grid.Row="0" Content="Check All" Click="CheckAllChecked" BorderBrush="Gray" IsChecked="{Binding IsAllChecked}"/>
                    </Border>
                    <Rectangle Height="1" Fill="{TemplateBinding BorderBrush}" Grid.Row="1" HorizontalAlignment="Stretch" />
                    <ListView Name="CompleteList" Grid.Row="2" Background="White" ItemsSource="{TemplateBinding ItemsSource}" BorderBrush="Transparent">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <CheckBox Checked="{Binding Path=IsSelected}" IsEnabled="{Binding Path=IsEnabled}">
                                    <Label Content="{Binding Path=Name}"/>
                                </CheckBox>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
                </Grid>
            </Border>
        </ControlTemplate>
    </ListView.Template>
</ListView>

我的视图绑定到Collection:

    public interface ICheckListItem
    {
        bool IsEnabled { get; set; }

        bool? IsSelected { get; set; }

        string Name { get; set; }
    }

我的测试数据如下:

    public List<myCollectionItem> Items { get; set; } = new List<myCollectionItem>
    {
        new myCollectionItem { IsEnabled = false, IsSelected = true, Name = "Object 1" },
        new myCollectionItem { IsEnabled = false, IsSelected = true, Name = "Object 1" },
        new myCollectionItem { IsEnabled = false, IsSelected = true, Name = "Object 1" },
    };

所以我的错误就是:

<ListView Name="CompleteList" Grid.Row="2" Background="White" ItemsSource="{TemplateBinding ItemsSource}" BorderBrush="Transparent">

1 个答案:

答案 0 :(得分:2)

而不是使用嵌套的ListView执行此操作,所有标准WPF项控制模板使用的“正确”方式是ItemsPresenter