WPF: Définition d'une liaison pour toutes les instances TreeViewItem
-
21-09-2019 - |
Question
Bonjour,
J'utilise WPF avec un modèle Model-View-ViewModel, et j'ai un modèle de vue avec une propriété IsSelected
que je veux lier à une propriété de TreeViewItem
de IsSelected
pour tous TreeViewItem
s dans le champ d'application. Je tente de le faire avec un Style
et un Setter
. Cela fonctionne apparemment pour les TreeViewItem
s niveau racine, mais pas pour leurs enfants. Pourquoi est-ce? Comment puis-je avoir cette applique à tous les contrôles TreeViewItem
?
Voici la vue 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>
La solution
Je pense que nous aurons besoin de plus d'informations pour répondre à votre question. Plus précisément, ce que votre modèle de vue (s) ressemblent. Voici un exemple que vous pouvez copier et coller qui fonctionne très bien.
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;
}
}
}
Autres conseils
travailler avec des modèles de vue, vous obtiendrez très sympathique avec la propriété ItemContainerStyle. ce que vous faites dans votre code défini le modèle de données pour le niveau de la racine. ce que vous voulez faire est de style chacun des éléments dans l'arborescence, que vous pouvez faire comme si.
<TreeView ItemsSource="{Binding TopLevelAreas}"
ItemContainerStyle="{StaticResource AreaTemplate}">
</TreeView>
profiter
Vous devez utiliser comme mentionné ci-dessous. Exploitez l'option BasedOn
<TreeView ItemsSource="{Binding TopLevelAreas}">
<TreeView.Resources>
<Style TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource StyleKey}"/>
</TreeView.Resources>
</TreeView>