因此,我试图构建一个具有“ CheckAll”按钮的炫酷ListView,该按钮可让我检查列表视图中的所有项目。然后,我可以在几个地方重用此控件。因此,我有一个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>


我的观点绑定到以下集合:

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

最佳答案

而不是使用嵌套的ListView,所有标准WPF项目控件模板使用的“正确”方式是ItemsPresenter

关于c# - ListView模板中的ListView不允许我绑定(bind)到父项ItemsSource,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44188595/

10-12 00:27
查看更多