本文介绍了如何有条件地插入定制的ListView列表项的复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尽量让一个自定义的ListView它如果需要用一些东西,最初的复选框填充每个列表项。当前未显示复选框,所以我想的东西ContentControl中我的code是莫名其妙的错误。
< ListView控件的xmlns =http://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml>
< ListView.View>
<&GridView的GT;
< GridViewColumn>
< GridViewColumn.CellTemplate>
<&DataTemplate的GT;
< - 每个列表项:[复选框]标签 - >
< StackPanel的方向=横向>
<! - 在code用于可选的复选框 - >
<&ContentControl中GT;
< ContentControl.Style>
<风格的TargetType =ContentControl中>
< Style.Triggers>
< DataTrigger绑定={结合IsCheckable}VALUE =真>
< setter属性=模板>
< Setter.Value>
<&控件模板GT;
<复选框器isChecked ={绑定路径= SomeProperty}/>
< /控件模板>
< /Setter.Value>
< /二传手>
< / DataTrigger>
< /Style.Triggers>
< /样式和GT;
< /ContentControl.Style>
< / ContentControl中>
<! - 非可选测试标签 - >
<标签内容=考试内容/>
< / StackPanel的>
< / DataTemplate中>
< /GridViewColumn.CellTemplate>
< / GridViewColumn>
< /ListView.View>
< /&的ListView GT;
在code背后:
公共部分类MyListView:{的ListView
公共MyListView(){
的InitializeComponent();
} 公共BOOL IsCheckable
{
{返回(布尔)的GetValue(IsCheckableProperty); }
集合{的SetValue(IsCheckableProperty,值); }
} 公共静态只读的DependencyProperty IsCheckableProperty =
DependencyProperty.Register(
IsCheckable
typeof运算(布尔)
typeof运算(AppropriatenessWidget)
新UIPropertyMetadata(假));
}
解决方案
{结合IsCheckable}
绑定到的DataContext
不控制,使用,例如:
{结合IsCheckable,
的RelativeSource = {的RelativeSource AncestorType =本地:MyListView}}
此外,我怀疑这子类的做法是一个好主意,这样可以很容易地通过处理一个基本的的DataContext
。
I try to make a customized ListView which fills each list item with some stuff and an initial Checkbox if desired. Currently no Checkbox is displayed so I guess my code of the ContentControl stuff is somehow erroneous.
<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>
The Code behind:
public partial class MyListView : ListView {
public MyListView () {
InitializeComponent();
}
public bool IsCheckable
{
get { return (bool)GetValue(IsCheckableProperty); }
set { SetValue(IsCheckableProperty, value); }
}
public static readonly DependencyProperty IsCheckableProperty =
DependencyProperty.Register(
"IsCheckable",
typeof(bool),
typeof(AppropriatenessWidget),
new UIPropertyMetadata(false));
}
解决方案
{Binding IsCheckable}
binds to the DataContext
not the control, use for example:
{Binding IsCheckable,
RelativeSource={RelativeSource AncestorType=local:MyListView}}
Also i doubt that this subclassing approach is such a good idea, this could easily be handled via an underlying DataContext
.
这篇关于如何有条件地插入定制的ListView列表项的复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!