質問

挨拶

私が使っているコンポーネントのラインナップとモデル/ビュー-ViewModelパターン、視モデル IsSelected 財いて触れたいと思い結合 TreeViewItem's IsSelected 物件のすべて TreeViewItemsの範囲においいること StyleSetter.この作品は一見の根レベルの TreeViewItemがないたします。これはなぜであろうか。方法を教えてくださいこなすべての TreeViewItem

こちらはビュー:

<UserControl x:Class="MyApp.AllAreasView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:MyApp="clr-namespace:MyApp"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="700">
<UserControl.Resources>
    <Style TargetType="{x:Type TreeViewItem}">
        <Setter Property="IsSelected"
                Value="{Binding IsSelected, Mode=TwoWay}"/>
    </Style>

    <MyApp:CountVisibilityConverter x:Key="CountVisibilityConverter" />

    <HierarchicalDataTemplate x:Key="AreaTemplate"
                              DataType="AreaViewModel"
                              ItemsSource="{Binding Path=SubareasCollectionView}">
        <WrapPanel>
            <TextBlock Text="{Binding Path=Name}" Margin="0 0 8 0" />
            <TextBlock DataContext="{Binding Path=Subareas}" 
                       Text="{Binding Path=Count, StringFormat= (\{0\})}"
                       Visibility="{Binding Path=Count, Converter={StaticResource CountVisibilityConverter}}" />
        </WrapPanel>
    </HierarchicalDataTemplate>
</UserControl.Resources>

<TreeView ItemsSource="{Binding TopLevelAreas}"
          ItemTemplate="{StaticResource AreaTemplate}">
</TreeView>

</UserControl>
役に立ちましたか?

解決

と思いま必要な詳細情報お答えします。具体的には、どのくモデルを紹介していこう。以下の例では、コピーして貼り付けできる製品です。

Window1.ー:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication1"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <Style TargetType="{x:Type TreeViewItem}">
            <Setter Property="Background" Value="{Binding Background}"/>
        </Style>

        <HierarchicalDataTemplate x:Key="ItemTemplate" DataType="local:DataItem" ItemsSource="{Binding Path=Children}">
            <TextBlock Text="{Binding Name}" />
        </HierarchicalDataTemplate>
    </Window.Resources>

    <TreeView ItemsSource="{Binding}" ItemTemplate="{StaticResource ItemTemplate}"/>
</Window>

Window1.ー.cs:

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Media;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            var dis = new ObservableCollection<DataItem>();
            var di = new DataItem() { Name = "Top", Background = Brushes.Red };
            di.Children.Add(new DataItem() { Name = "Second", Background = Brushes.Blue });

            dis.Add(di);
            DataContext = dis;
        }
    }

    public class DataItem
    {
        public DataItem()
        {
            Children = new ObservableCollection<DataItem>();
        }

        public string Name
        {
            get;
            set;
        }

        public ICollection<DataItem> Children
        {
            get;
            set;
        }

        public Brush Background
        {
            get;
            set;
        }
    }
}

他のヒント

ビューモデルで作業するあなたは、ItemContainerStyleプロパティと非常にフレンドリーれます。あなたがあなたのコードでやっていたことは、ルートレベルのデータテンプレートを設定します。何がやりたいことは、あなたがそのように行うことができますスタイルツリービューの各項目、である。

<TreeView ItemsSource="{Binding TopLevelAreas}"
          ItemContainerStyle="{StaticResource AreaTemplate}">
</TreeView>

タグ楽しみます

あなたは、後述するように使用する必要があります。 BASEDONオプション

を利用します
<TreeView ItemsSource="{Binding TopLevelAreas}">
   <TreeView.Resources>
            <Style TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource StyleKey}"/>
   </TreeView.Resources>
</TreeView>
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top