2012-09-12 39 views
1

我有一個WPF樹視圖。下面是代碼:如何在樹視圖中使用依賴屬性和對象來更新樹視圖以在對象中顯示更改的值

<DockPanel Grid.Row="2" Margin="0,6,0,0" Grid.RowSpan="5" Height="491" VerticalAlignment="Top"> 
     <DockPanel.Resources> 

      <src:TreeViewFilter x:Key="MyList" /> 

      <HierarchicalDataTemplate DataType="{x:Type src:TreeViewParent}" ItemsSource="{Binding Path=OrderAttributes}"> 
       <TextBlock Text="{Binding Path=Name}" FontSize="16"/> 
      </HierarchicalDataTemplate> 

      <HierarchicalDataTemplate DataType="{x:Type src:OrderAttribute}" ItemsSource="{Binding Path=OrderAttributes}"> 
       <TextBlock Text="{Binding Path=NameAndCount}" /> 
      </HierarchicalDataTemplate> 

     </DockPanel.Resources> 
     <TreeView Name="treeView1" Height="490" Width="235" VerticalAlignment="Top" ItemsSource="{Binding Source={StaticResource MyList}, UpdateSourceTrigger=PropertyChanged}" TreeViewItem.Selected="treeViewFilter" /> 
    </DockPanel> 

樹視圖結合到靜態資源MYLIST和TreeViewParents對象在TreeViewFilter類創建:(除去一些不必要的SQL代碼來檢索數據)。

public class TreeViewFilter : ObservableCollection<TreeViewParent> 
{ 
    //three tree view parents that wont change 
    public TreeViewParent allOrders; 
    public TreeViewParent batchStatus; 
    public TreeViewParent shippedOrders; 
    static TreeViewFilter currentInstance1; //maybe set to null, can only create one instance! 

    public TreeViewFilter() 
    { 

     currentInstance1 = this; 

     //Create and fill out all orders tree filter 
     Add(allOrders = new TreeViewParent("All Orders", 0)); 

     //Create and fill out batch status tree filter 
     Add(batchStatus = new TreeViewParent("Batch Status", 0)); 
     int untouchedCount, batchReadyCount, errorCount; 

     OrderAttribute untouched = new OrderAttribute("Untouched", "Batch Status", 3, untouchedCount); 
     OrderAttribute batchReady = new OrderAttribute("Batch Ready", "Batch Status", 3, batchReadyCount); 
     OrderAttribute error = new OrderAttribute("Error", "Batch Status", 3, errorCount); 
     batchStatus.OrderAttributes.Add(untouched); 
     batchStatus.OrderAttributes.Add(batchReady); 
     batchStatus.OrderAttributes.Add(error); 

     OrderManager currentInstance = OrderManager.getCurrentInstance(); 
    } 

    public static TreeViewFilter getCurrentInstance() 
    { 
     return currentInstance1; 
    } 
} 

然後樹視圖父母綁定到訂單屬性。訂單屬性也可以有自己的訂單屬性集合。 (分層過濾樹視圖)

這裏是TreeViewParent和OrderAttribute編碼:(均爲similiar)

public class Base 
{ 
    public int classIdentifier; 
} 

公共類TreeViewParent:基 { 靜態TreeViewParent currentInstance;

public TreeViewParent(string name, int classIdent) 
    { 
     this._name = name; 
     this._orderAttributes = new ObservableCollection<OrderAttribute>(); 
     classIdentifier = classIdent; 
     currentInstance = this; 
    } 

    public string _name; 

    public string Name { get { return _name; } } 

    ObservableCollection<OrderAttribute> _orderAttributes; 
    public ObservableCollection<OrderAttribute> OrderAttributes 
    { 
     get { return _orderAttributes; } 
    } 

    public static TreeViewParent getCurrentInstance() 
    { 
     return currentInstance; 
    } 
} 

公共類OrderAttribute:基 { 公共字符串parentFilter; static OrderAttribute currentInstance;

public OrderAttribute(string name, string parent, int classIdent) 
    { 
     _name = name; 
     parentFilter = parent; 
     classIdentifier = classIdent; 
     _orderAttributes = new ObservableCollection<OrderAttribute>(); 
     currentInstance = this; 
    } 

    public OrderAttribute(string name, string parent, int classIdent, int count) 
    { 
     _name = name; 
     parentFilter = parent; 
     classIdentifier = classIdent; 
     _count = count; 
     currentInstance = this; 
    } 

    string _name; 
    public int _count = 0; 

    public string Name { get { return _name; } } 

    public string NameAndCount 
    { 
     get 
     { 
      if (_count == 0) 
      { 
       return _name; 
      } 
      else 
      { 
       return _name + " (" + _count + ")"; 
      } 
     } 
    } 

    ObservableCollection<OrderAttribute> _orderAttributes; 
    public ObservableCollection<OrderAttribute> OrderAttributes { get { return _orderAttributes; } } 

    public static OrderAttribute getCurrentInstance() 
    { 
     return currentInstance; 
    } 
} 

我如何使用相關對象已經更新NameAndCount數據顯示和動態改變的程序運行和計數改變?

回答

0

該視圖現在應該何時以及如何改變OrderAttribute對象的NameAndCount屬性。

您必須實施INotifyPropertyChanged界面並在每次想要向您的視圖發送通知時提升PropertyChanged事件。

在您的示例中,_count字段值發生更改。

public OrderAttribute : Base, INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    protected void FirePropertyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 

    private int _count = 0; 

    public int Count 
    { 
     get { return _count; } 
     set 
     { 
      if(_count != value) 
      { 
       _count = value; 
       FirePropertyChanged("NameAndCount"); 
      } 
     } 
    } 
} 
+0

感謝這工作!沒想到有人會用這麼多的代碼發表回覆......非常感謝! –

+0

另一個問題,我想做幾乎完全相同的事情,但用布爾值而不是字符串。這是如何改變這個例子的? –

相關問題