2009-11-21 29 views
1

問候,WPF:設置爲所有樹型視圖例如

我使用WPF與模型 - 視圖 - 視圖模型模式綁定,我有與IsSelected屬性視圖模型,我想綁定到一個TreeViewItem的範圍內的所有TreeViewItem的財產IsSelected。我試圖用StyleSetter來做到這一點。這顯然適用於根級TreeViewItem s,但不適合他們的孩子。爲什麼是這樣?我怎麼能適用於所有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> 

回答

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; 
     } 
    } 
} 
+0

感謝您的工作示例(並推斷簡單視圖模型)。事實證明,我已經生成了兩組獨立的視圖模型,並且我的視圖模型中的PropertyChanged事件沒有正確處理,因爲我已經掛鉤了一個,而WPF則是另一個。跟蹤事件並檢查PropertyChanged委託的GetInvocationList()的結果,讓我找到它。既然你證明它有效,我會接受這個答案。 – codekaizen 2009-11-22 08:23:32

0

與視圖模型的工作,你會得到非常友好與ItemContainerStyle屬性。你在代碼中所做的是爲根級設置數據模板。你想要做的就是設計樹形視圖中的每個項目,你可以這樣做。

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

享受

0

你有如下所述使用。利用BasedOn選項

<TreeView ItemsSource="{Binding TopLevelAreas}"> 
    <TreeView.Resources> 
      <Style TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource StyleKey}"/> 
    </TreeView.Resources> 
</TreeView> 
+0

感謝您復活這個,但正如接受的答案中指出的,我的問題是一個不正確的DataContext:我沒有從視圖模型獲取屬性更改通知,因爲我沒有在視圖上正確設置它。這種模式不需要在樣式中設置'BasedOn',事實上,由於'StyleKey'在資源字典中不存在,這會破壞它。 – codekaizen 2015-12-15 09:02:06