问题
I try to make a customized ListView which fills each list item with some stuff and an initial Checkbox if the concrete inheriting class wishes so. Currently no Checkbox is displayed so I guess my code of the ContentControl stuff is somehow erroneous.
<UserControl x:Class="local:MyListView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="some/path/here">
<ListView>
<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, RelativeSource={RelativeSource AncestorType=local:MyListView}}"
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>
</UserControl>
The Code behind:
abstract partial class MyListView {
protected MyListView () {
InitializeComponent();
}
protected abstract bool IsCheckable { get; }
}
// A checkbox should be displayed - but it's not...
public class MyListView1 : MyListView {
protected bool IsCheckable { get { return true; } }
}
public class MyListView2 : MyListView {
protected bool IsCheckable { get { return false; } }
}
When inspecting the output window I discovered following message (that I don't know how to make use of):
System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=IsCheckable; DataItem='MyListView2' (Name=''); target element is 'ContentControl' (Name=''); target property is 'NoTarget' (type 'Object')
System.Windows.Data Error: 39 : BindingExpression path error: 'IsCheckable' property not found on 'object' ''MyListView2' (Name='')'. BindingExpression:Path=IsCheckable; DataItem='MyListView2' (Name=''); target element is 'ContentControl' (Name=''); target property is 'NoTarget' (type 'Object')
System.Windows.Data Information: 19 : BindingExpression cannot retrieve value due to missing information. BindingExpression:Path=IsCheckable; DataItem='MyListView2' (Name=''); target element is 'ContentControl' (Name=''); target property is 'NoTarget' (type 'Object')
System.Windows.Data Information: 20 : BindingExpression cannot retrieve value from null data item. This could happen when binding is detached or when binding to a Nullable type that has no value. BindingExpression:Path=IsCheckable; DataItem='MyListView2' (Name=''); target element is 'ContentControl' (Name=''); target property is 'NoTarget' (type 'Object')
The same error messages appear for MyListView1. Note that this question arose from the evolution of an older post.
回答1:
According to the msdn Binding Sources Overview:
The properties you use as binding source properties for a binding must be public properties of your class. Explicitly defined interface properties cannot be accessed for binding purposes, nor can protected, private, internal, or virtual properties that have no base implementation.
来源:https://stackoverflow.com/questions/12411478/how-to-conditionally-insert-a-checkbox-in-a-list-item-of-a-customized-listview-w