다음과 같이 계층 구조 클래스 구조를 사용하여 트리 뷰를 바인딩합니다.트리 뷰 항목의 이미지를 변경하는 Wpf 트리거
스토어 -> ImagePath를 -> 목록 -> 목록
나는 사람에 대한 DataTemplate을을 만드는 오전, 나는에 선언 된 person.name 의 조합 이미지 경로를 사용하려면 저장. 다음은 MainWindow.xaml 파일의 코드입니다. `public partial class MainWindow : Window { public MainWindow() { InitializeComponent();
Customers customers = new Customers();
customers.Users = new List<Person>
{
new Person { Name = "John"},
new Person { Name = "Adam"},
new Person { Name = "Smith"}
};
Store root = new Store();
root.ImagePath = "imageone.png";
root.Add(customers);
this.DataContext = root;
}
}
public class Store : ObservableCollection<Customers>
{
public string ImagePath
{
get;
set;
}
}
public class Customers
{
public string Label
{
get
{
return string.Format("People({0})", Users.Count());
}
}
public List<Person> Users
{
get;
set;
}
}
public class Person
{
public string Name
{
get;
set;
}
}`
여기에 xaml이 있으며이 원본 = "{Binding Store.ImagePath}"는 작동하지 않습니다.
<Window.Resources >
<DataTemplate DataType="{x:Type local:Person}" x:Key="personKey" >
<StackPanel Orientation="Horizontal" >
<Image Source="{Binding Store.ImagePath}"></Image>
<TextBlock Text="{Binding Name}" />
</StackPanel>
</DataTemplate>
<HierarchicalDataTemplate x:Key="customerKey" ItemsSource="{Binding Users}" ItemTemplate="{StaticResource personKey }" >
<TextBlock Text="{Binding Label}" FontWeight="Bold"/>
</HierarchicalDataTemplate>
</Window.Resources>
<Grid>
<Canvas>
<Button HorizontalAlignment="Left" DockPanel.Dock="Top" Height="29" Width="112" Canvas.Left="123" Canvas.Top="5">Image one</Button> <Button HorizontalAlignment="Left" VerticalAlignment="Top" DockPanel.Dock="Top" Height="28" Width="119" Canvas.Left="249" Canvas.Top="7">Image two</Button>
<TreeView HorizontalAlignment="Stretch" Name="treeView1" VerticalAlignment="Stretch"
ItemsSource="{Binding .}" ItemTemplate="{StaticResource customerKey}" Height="260" Width="363" Canvas.Left="81" Canvas.Top="45" />
</Canvas>
</Grid>
버튼을 클릭하면 programaticaaly 이미지를 변경하고 모든 사람 treeview 항목이 변경됩니다.
감사