Wpf 如果使用模板创建ListView,则不选择编辑项
这是我的XAML:Wpf 如果使用模板创建ListView,则不选择编辑项,wpf,templates,listview,selection,Wpf,Templates,Listview,Selection,这是我的XAML: <Window x:Class="Test.MainWindow".... DataContext="{Binding RelativeSource={RelativeSource Self}}"...> <Window.Resources> <ResourceDictionary> <ControlTemplate x:Key="lvTemplate" TargetType="
<Window x:Class="Test.MainWindow"....
DataContext="{Binding RelativeSource={RelativeSource Self}}"...>
<Window.Resources>
<ResourceDictionary>
<ControlTemplate x:Key="lvTemplate" TargetType="{x:Type ListView}">
<ListView Width="200" ItemsSource="{TemplateBinding ItemsSource}">
<ListView.View>
<GridView>
<GridViewColumn Width="140" Header="Name"
DisplayMemberBinding="{Binding name}" />
</GridView>
</ListView.View>
</ListView>
</ControlTemplate>
</ResourceDictionary>
</Window.Resources>
<Grid>
<ListView x:Name="lview" Template="{StaticResource lvTemplate }"
ItemsSource="{Binding collection}".../>
<ListView x:Name="lview2" ItemsSource="{Binding collection2}"...>
<ListView.View>
<GridView>
<GridViewColumn Width="140" Header="Name"
DisplayMemberBinding="{Binding name}" />
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
因此,对于两个ListView,它们之间唯一的区别是第一个是使用模板创建的
如果我将元素添加到集合中,两个列表中都会出现新行,并且看起来一切正常。但是,当我在第一个ListView(lview)中选择一行时,没有选择任何项目(只是在视觉上它看起来是选中的,在SelectedIndex=-1、SelectedItem==null后面的代码中)。更奇怪的是,此ListView的View属性为null。
这里怎么了
万斯谢谢你 如果您想在多个
列表视图
之间共享视图,您可以创建GridView
resource,方法是:
为什么要用另一个
ListView
构建ListView的模板,在那里更改视图
?为什么不简单地更改主列表视图的视图
?这两个网格是独立的,因此没有选择或视图在它们之间传输。我在这里显示一个简化的代码。在实际应用程序中,大约有十个选项卡项,每个选项卡项都有一个具有相同8列的ListView。我只是不想让我的XAML有几米长,如果我以后不得不更改某些内容(例如列名),我只想更改一次。我选择了可能不是最优雅的方式。我的目的是定义所有这些列(我选择了一个模板),然后在我的ListView中引用它。好的,我用另一种方式问我的问题:如何只定义一次GridView(或列定义),然后重用它?dkozl,再次感谢!这正是我要找的!
<GridView x:Key="gridView" x:Shared="False">
<GridViewColumn Width="140" Header="Name" DisplayMemberBinding="{Binding name}" />
</GridView>
<ListView x:Name="lview" ItemsSource="{Binding collection}" View="{StaticResource gridView}"/>
<ListView x:Name="lview2" ItemsSource="{Binding collection2}" View="{StaticResource gridView}"/>