- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
问候,
我正在使用带有 Model-View-ViewModel 模式的 WPF,并且我有一个带有 IsSelected
的 View 模型我想绑定(bind)到 TreeViewItem
的属性的IsSelected
所有人的属性(property)TreeViewItem
s 范围内。我正在尝试使用 Style
和 Setter
.这显然适用于根级别 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>
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/
C#: TreeViewItem treeItem = null; treeItem = new TreeViewItem(); treeItem.Header = "
我有 webservice asmx,还有类: Country public string Name {get;set;} public string Code {get;set;} public L
我有一个Silverlight树 View ,该树 View 动态填充有treeviewitems(代表 View )。我的工作正常,但是当我单击Treeviewitem时说“ View 2.01”时
我使用最新版本的 mvvm light 工具包,但是我不清楚如何将 EventToCommand 用于事件 TreeViewItem.Expanded。 这很有效......我做错了什么?
当 TreeView(或应用程序)失去焦点时,如何更改所选 TreeViewItem 的背景。在这种情况下,默认情况下选定的项目具有浅灰色背景。 编辑:第一个答案后的尝试:但是找不到带有 Target
我希望能够通过右键单击在我的程序中选择一个 TreeViewItem。以前,(在 this question 中)我试图通过从任何我想允许选择 TreeViewItem 的地方调用 SetSelect
我是 WPF 表单的新手,在尝试在 TreeViewItem 中设置背景时遇到了一个问题。
我被要求更新 WPF 应用程序中的 TreeVeiw,该应用程序是从类对象动态构建的。正如您将看到的,treeveiw 不受任何约束。以下不是我的代码!! private void Buil
目前这是我的控件: 它应该在 ListBox 中,但问题是我需要能够仅选择父项或子项。 这是一个简单的概念,但我已经尝试了很多(包装在 ListBoxItem 中。使用扩展器并手动更改可
我希望能够从 TreeViewItem 中删除子节点通过按键盘上的删除按钮。我正在使用 SelectedItem尝试这样做。 目前我正在使用 KeyDown我的代码隐藏中的事件: private vo
private void SetCurrentItemInEditMode(bool EditMode) { if (product_tree.SelectedItem is Tre
你好,我正在使用一个 WPF 应用程序,我希望在展开 TreeView 上的项目之前触发事件。我知道目前还没有为此举办的事件,但我不确定如何以我这样做的方式添加一个事件,因此我们将不胜感激。 这是 x
我有一个 TreeView,我从一个集合中填充了一个 TreeViewItem 列表(使用绑定(bind)),我链接到一个 TreeView 事件“展开”以展开它并显示他们的 child ,我现在的目
您好问题是我有多个 TreeView 控件,每个 TreeView 都有自己的 TreeViewItem 样式,设置它 TreeView ItemContainerStyle="{StaticReso
我正在尝试创建一个向右水平扩展的 TreeView ,以便它像这样扩展: Parent1 -> Child1 Child2 Child3
为了在 TreeViewItem 中存储一个对象(比如一个类的实例),我目前将该对象存储在 TreeViewItem 中。的 Header然后覆盖 ToString该类的方法,以便它显示正确的字符串标
在我的 MVVM 应用程序中,我有一个树 View ,应该在选择时将树 View 项带入 View 。树 View 表示数据库中的记录。每个树 View 项通过在选择时展开项来按需加载其子项。 树 V
我有一个典型的 TreeView 和一个 View 模型。 View 模型有一个可观察的其他 View 模型集合,用作树的数据源。 public class TreeViewVM { publ
我有一个 TreeView我正在尝试实现一种样式,允许我使用 HierarchicalDataTemplate 在特定节点的所有子节点周围放置边框.我想要的一个例子如下所示: 以下代码是我到目前为止所
我在 TreeView 中有一个 TreeView 项目,它将绑定(bind)一个列表:
我是一名优秀的程序员,十分优秀!