Wpf 如何在自定义ListView的列表项中有条件地插入复选框?
我尝试创建一个定制的ListView,它用一些东西填充每个列表项,如果需要的话,还可以创建一个初始复选框。目前并没有显示复选框,所以我猜我的ContentControl代码有点错误Wpf 如何在自定义ListView的列表项中有条件地插入复选框?,wpf,listview,data-binding,controltemplate,contentcontrol,Wpf,Listview,Data Binding,Controltemplate,Contentcontrol,我尝试创建一个定制的ListView,它用一些东西填充每个列表项,如果需要的话,还可以创建一个初始复选框。目前并没有显示复选框,所以我猜我的ContentControl代码有点错误 <ListView xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <ListV
<ListView xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ListView.View>
<GridView>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<!-- Each list item: [Checkbox] Label -->
<StackPanel Orientation="Horizontal">
<!-- The code for the optional check box -->
<ContentControl>
<ContentControl.Style>
<Style TargetType="ContentControl">
<Style.Triggers>
<DataTrigger Binding="{Binding IsCheckable}" Value="True">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<CheckBox IsChecked="{Binding Path=SomeProperty}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
<!-- The non-optional test label -->
<Label Content="Test Content" />
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</ListView.View>
</ListView>
{Binding IsCheckable}
绑定到DataContext
而不是控件,例如:
{绑定是可检查的,
RelativeSource={RelativeSource AncestorType=local:MyListView}
我还怀疑这种子类化方法是否是一个好主意,它可以通过底层的
DataContext
轻松处理,调用XAML代码HTML是一种侮辱。还有其他提示吗?myitemsource
构造非常可怕,如果您使用itemsource
继承某个表单,为什么会存在这种情况呢?(如果有任何东西使用lang xml
或仅xaml
作为语言提示。)有效点!我刚刚删除了MyItemsSource(代码与原代码略有不同)。谢谢!我不记得我已经浪费了多少时间绑定DataContext导致了问题!如果只有一种方法可以调试不正确的WPF绑定(除了刚才看到的错误)…@Bastian:。再次感谢!明天我将尝试这篇文章。请参考您关于子类化与使用DataContext
的评论。与前者相比,后者有什么好处?@Bastian:你不需要一个什么都不做的新类。