gpt4 book ai didi

WPF:为所有 TreeViewItem 实例设置绑定(bind)

转载 作者:行者123 更新时间:2023-12-03 10:24:52 24 4
gpt4 key购买 nike

问候,

我正在使用带有 Model-View-ViewModel 模式的 WPF,并且我有一个带有 IsSelected 的 View 模型我想绑定(bind)到 TreeViewItem 的属性的IsSelected所有人的属性(property)TreeViewItem s 范围内。我正在尝试使用 StyleSetter .这显然适用于根级别 TreeViewItem s,但不是为了他们的 child 。为什么是这样?我怎样才能让它适用于所有 TreeViewItem控制?

这是 View 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>

最佳答案

我想我们需要更多信息来回答你的问题。具体来说,您的 View 模型是什么样的。下面是一个可以正常复制和粘贴的示例。

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

关于WPF:为所有 TreeViewItem 实例设置绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1774310/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com