1

MVVM 監視可能なコレクションにバインドされた TreeView があります。私のアイテムテンプレートは、次のコードが示すように、画像とテキストブロックで構成されています:

<HierarchicalDataTemplate x:Key="TreeViewItemTemplate" ItemsSource="{Binding Items, Mode=OneWay, NotifyOnSourceUpdated=True}">
    <TreeViewItem>
        <TreeViewItem.Header>
            <StackPanel Orientation="Horizontal">
                <Image 
                    Margin="-20,0,5,0"
                    Source="{Binding Icon, Converter={StaticResource TreeViewIconConverter}, Mode=OneWay}" 
                    Style="{DynamicResource SmallIcon}"/>
                <Label Content="{Binding Label}"/>
            </StackPanel>
        </TreeViewItem.Header>
    </TreeViewItem>
</HierarchicalDataTemplate>

アイテムをクリックすると問題が発生します。マウス カーソルが StackPanel の上にある場合、選択は行われません。これをより明確にするために、スクリーンショットも含めました。 ここに画像の説明を入力

もちろん、これは StackPanel が選択領域の上にあるために発生します。

回避策はありますか?

4

1 に答える 1

1

I found the answer by myself. When you customize the TreeView using an hierarchical data template then you should not replicate the TreeViewItem.Header template because at runtime WPF will create one for you. So, in order to have a custom TreeViewItem this code is enough:

<HierarchicalDataTemplate x:Key="TreeViewItemTemplate" ItemsSource="{Binding Items, Mode=OneWay, NotifyOnSourceUpdated=True}">
    <StackPanel Orientation="Horizontal">
        <Image 
            Margin="0,0,5,0"
            Source="{Binding Icon, Converter={StaticResource TreeViewIconConverter}, Mode=OneWay}" 
            Style="{DynamicResource SmallIcon}"/>
        <Label Content="{Binding Label}"/>
    </StackPanel>
</HierarchicalDataTemplate>
于 2013-01-22T15:05:55.687 に答える