0

必要に応じて、各リスト項目にいくつかのものと最初のチェックボックスを入力するカスタマイズされた ListView を作成しようとしています。現在、チェックボックスは表示されていないので、私の ContentControl のコードは何らかの形で間違っていると思います。

<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>

コードビハインド:

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));
}
4

1 に答える 1

0

{Binding IsCheckable}DataContextコントロール以外にバインドします。たとえば、次のように使用します。

{Binding IsCheckable,
         RelativeSource={RelativeSource AncestorType=local:MyListView}}

また、このサブクラス化アプローチが非常に良いアイデアであるとは思えません。これは、基になる を介して簡単に処理できますDataContext

于 2012-08-30T17:49:15.703 に答える