具体的な継承クラスが希望する場合は、各リスト項目をいくつかのものと初期チェックボックスで埋めるカスタマイズされた ListView を作成しようとします。現在、チェックボックスは表示されていないので、私の ContentControl のコードは何らかの形で間違っていると思います。
<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>
コードビハインド:
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; } }
}
出力ウィンドウを調べると、次のメッセージが表示されました (使用方法がわかりません)。
System.Windows.Data 情報: 10 : バインディングを使用して値を取得できず、有効なフォールバック値が存在しません。代わりにデフォルトを使用します。BindingExpression:Path=IsCheckable; DataItem='MyListView2' (Name=''); ターゲット要素は 'ContentControl' (Name='') です。ターゲット プロパティは 'NoTarget' (タイプ 'Object') です
System.Windows.Data エラー: 39: BindingExpression パス エラー: 'IsCheckable' プロパティが 'object' ''MyListView2' (Name='')' に見つかりません。BindingExpression:Path=IsCheckable; DataItem='MyListView2' (Name=''); ターゲット要素は 'ContentControl' (Name='') です。ターゲット プロパティは 'NoTarget' (タイプ 'Object') です
System.Windows.Data 情報: 19 : 情報が欠落しているため、BindingExpression は値を取得できません。BindingExpression:Path=IsCheckable; DataItem='MyListView2' (Name=''); ターゲット要素は 'ContentControl' (Name='') です。ターゲット プロパティは 'NoTarget' (タイプ 'Object') です
System.Windows.Data 情報: 20 : BindingExpression は null データ項目から値を取得できません。これは、バインディングがデタッチされている場合、または値を持たない Nullable 型にバインドしている場合に発生する可能性があります。BindingExpression:Path=IsCheckable; DataItem='MyListView2' (Name=''); ターゲット要素は 'ContentControl' (Name='') です。ターゲット プロパティは 'NoTarget' (タイプ 'Object') です
MyListView1 についても同じエラー メッセージが表示されます。この質問は、古い投稿の進化から生じたことに注意してください。