ListView 模板中的 ListView 不允许我绑定到父 ItemsSource
ListView inside a ListView Template not allowing me to bind to the parents ItemsSource
所以我正在尝试构建一个很酷的 ListView,它有一个 "CheckAll" 按钮,可以让我检查列表视图中的所有项目。然后我可以在几个地方重用这个控件。所以我有一个 ControlTemplate,我试图将新的 ListView 的 ItemsSource 属性 绑定到父控件 ItemsSource 属性。如果我设置 TargetType="ListView" 让我定义它,但随后我得到一个无法使用它的 XML 解析异常。
<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">
所有标准 WPF 项控件模板使用的 "correct" 方式不是使用嵌套 ListView
,而是 ItemsPresenter
。
所以我正在尝试构建一个很酷的 ListView,它有一个 "CheckAll" 按钮,可以让我检查列表视图中的所有项目。然后我可以在几个地方重用这个控件。所以我有一个 ControlTemplate,我试图将新的 ListView 的 ItemsSource 属性 绑定到父控件 ItemsSource 属性。如果我设置 TargetType="ListView" 让我定义它,但随后我得到一个无法使用它的 XML 解析异常。
<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">
所有标准 WPF 项控件模板使用的 "correct" 方式不是使用嵌套 ListView
,而是 ItemsPresenter
。