【问题标题】:ListView inside a ListView Template not allowing me to bind to the parents ItemsSourceListView 模板内的 ListView 不允许我绑定到父项 ItemsSource
【发布时间】:2017-10-26 13:57:22
【问题描述】:

所以我正在尝试构建一个很酷的 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>

我的视图绑定到以下集合:

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

【问题讨论】:

  • 为什么不使用ItemsPresenter?这就是他们的目的。
  • 为什么不确实...修复它。谢谢!

标签: c# wpf xaml listview templatebinding


【解决方案1】:

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

【讨论】:

  • 所以我知道关于项目演示者的事情......但我只是在做一些愚蠢的事情。 /叹气无论如何我会投票给你的答案......但我从来没有得到超过1个代表,哈哈。再次感谢。
猜你喜欢
  • 2011-01-14
  • 1970-01-01
  • 1970-01-01
  • 2018-08-02
  • 2012-01-08
  • 2018-11-23
  • 2014-12-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多