[英]Bound treeview partially refreshing
我做了一個鏡頭應用來舉例說明我的問題:
繼承人window1.xaml
<Window
x:Class="TreeViewStepByStep.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
Width="400"
Height="600">
<Window.Resources />
<Grid>
<TreeView
Margin="0,21,0,0"
ItemsSource="{Binding Regions}"
HorizontalAlignment="Left"
Width="221">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate
ItemsSource="{Binding Type}">
<TextBlock
Text="{Binding Name}" />
<HierarchicalDataTemplate.ItemTemplate>
<HierarchicalDataTemplate
ItemsSource="{Binding Locations}">
<TextBlock
Text="{Binding Name}" />
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<TextBlock
Text="{Binding}" />
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
<Button
Content="Populate"
Height="23"
HorizontalAlignment="Left"
Margin="227,21,0,0"
Name="button2"
VerticalAlignment="Top"
Width="96"
Click="button2_Click" />
<Button
Content="Add child"
Height="23"
HorizontalAlignment="Left"
Margin="227,49,0,0"
Name="button3"
VerticalAlignment="Top"
Width="96"
Click="button3_Click" />
<Button
Content="Modify"
Height="23"
HorizontalAlignment="Left"
Margin="227,106,0,0"
Name="button4"
VerticalAlignment="Top"
Width="96"
Click="button4_Click" />
<Button
Content="Add root level"
Height="23"
HorizontalAlignment="Left"
Margin="227,135,0,0"
Name="button5"
VerticalAlignment="Top"
Width="96"
Click="button5_Click" />
</Grid>
</Window>
這是window1.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
namespace TreeViewStepByStep
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
Collection<Region> regions;
public Window1()
{
InitializeComponent();
}
private void button2_Click(object sender, RoutedEventArgs e)
{
Region sms = new Region("São Mateus do Sul")
{
Type =
{
new Types("Placemarks")
{
Locations =
{
"Pine trees",
"Barn",
"Phantom city"
}
},
new Types("Planning")
{
Locations =
{
"Geada",
"Por do sol"
}
},
}
};
Region others = new Region("Outros")
{
Type =
{
new Types("Placemarks")
{
Locations =
{
"Road",
"Waterfall"
}
},
new Types("Planning")
{
Locations =
{
"Moon"
}
},
}
};
regions = new Collection<Region>() { sms, others };
DataContext = new
{
Regions = regions
};
}
private void button3_Click(object sender, RoutedEventArgs e)
{
this.regions[1].Type.Add(new Types("New folder")
{
Locations =
{
"Test"
}
}
);
}
private void button4_Click(object sender, RoutedEventArgs e)
{
this.regions[0].Name = "Edited";
}
private void button5_Click(object sender, RoutedEventArgs e)
{
this.regions.Add(new Region("My new region"));
}
}
public class Region
{
public Region(string name)
{
Name = name;
Type = new ObservableCollection<Types>();
}
public string Name { get; set; }
public ObservableCollection<Types> Type { get; set; }
}
public class Types
{
public Types(string name)
{
Name = name;
Locations = new ObservableCollection<string>();
}
public string Name { get; private set; }
public ObservableCollection<string> Locations { get; set; }
}
}
我的TreeView
綁定到層次模型(區域變量)。 當我單擊“填充”時,它將TreeView
綁定到此變量。 當我單擊“添加子”時,它會將一個子元素添加到綁定變量中,並在TreeView
上反映出來。 到現在為止還挺好。
當我嘗試修改現有元素的名稱或嘗試添加根級別節點(“修改”和“添加根級別”按鈕)時,會出現問題。 這些修改不應該反映在TreeView
,因為它與集合綁定了嗎?
正如您現在所擁有的那樣, Collection
在更改時不會通知視圖。 您想使用ObservableCollection而不是普通的Collection<T>
。 ObservableCollection
被編碼為與UI一起使用,並確保通過CollectionChanged
事件始終使用這些更改更新視圖。 您可以將ObservableCollection
與其他一些一起使用,但您的Regions變量是一個Collection。
您需要將Collection
類型更改為ObservableCollection
並需要在區域類上實現INotifyPropertyChanged
以在編輯時向View發送通知。
C#窗口類:
ObservableCollection<Region> regions;
地區分類:
public class Region : INotifyPropertyChanged
{
public Region(string name)
{
Name = name; Type = new ObservableCollection<Types>();
}
private string name;
public string Name
{
get { return name; }
set
{
name = value;
PropertyChanged(this, new PropertyChangedEventArgs("Name"));
}
}
public ObservableCollection<Types> Type { get; set; }
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged = (s, e) => { };
#endregion
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.