Вопрос

Привет,

Я использую WPF с шаблоном Model-View-ViewModel, и у меня есть модель представления с IsSelected свойство, которое я хочу привязать к TreeViewItem's IsSelected собственность для всех TreeViewItems в области видимости.Я пытаюсь сделать это с помощью Style и Setter.Очевидно, это работает для корневого уровня TreeViewItems, но не для своих детей.Почему это?Как я могу применить это ко всем TreeViewItem контролирует?

Вот представление XAML:

<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>
Это было полезно?

Решение

Я думаю, нам понадобится дополнительная информация, чтобы ответить на ваш вопрос.В частности, как выглядят ваши модели представления.Ниже приведен пример, который вы можете скопировать и вставить, и он отлично работает.

Окно1.xaml:

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

Окно1.xaml.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