1

问候,

我正在使用带有 Model-View-ViewModel 模式的 WPF,并且我有一个带有IsSelected属性的视图模型,我想将其绑定到范围内所有 s 的属性TreeViewItem。我试图用 a和 a来做到这一点。这显然适用于根级别s,但不适用于他们的孩子。为什么是这样?我怎样才能将其应用于所有控件?IsSelectedTreeViewItemStyleSetterTreeViewItemTreeViewItem

这是视图 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>
4

3 回答 3

3

我想我们需要更多信息来回答你的问题。具体来说,您的视图模型是什么样的。下面是一个可以正常复制和粘贴的示例。

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

Window1.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;
        }
    }
}
于 2009-11-21T09:13:41.953 回答
0

使用视图模型,您将对 ItemContainerStyle 属性非常友好。您在代码中所做的设置为根级别的数据模板。您要做的是为树视图中的每个项目设置样式,您可以这样做。

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

请享用

于 2009-11-21T14:32:10.183 回答
0

你必须使用如下所述。使用 BasedOn 选项

<TreeView ItemsSource="{Binding TopLevelAreas}">
   <TreeView.Resources>
            <Style TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource StyleKey}"/>
   </TreeView.Resources>
</TreeView>
于 2015-12-15T08:08:42.743 回答