C# 无法将TreeViewItem的IsSelected属性绑定到ViewModel
在这里找到了这么多答案,但没有一个对我有用。这是我的XAMLC# 无法将TreeViewItem的IsSelected属性绑定到ViewModel,c#,wpf,mvvm,C#,Wpf,Mvvm,在这里找到了这么多答案,但没有一个对我有用。这是我的XAML <UserControl x:Class="Helper.View.TreeViewUserControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x
<UserControl x:Class="Helper.View.TreeViewUserControl"
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"
mc:Ignorable="d"
xmlns:this="clr-namespace:Helper.Model"
xmlns:vm="clr-namespace:Helper.ViewModel"
DataContext="{DynamicResource TreeNodeViewModel}">
<UserControl.Resources>
<vm:TreeNodeViewModel x:Key="TreeNodeViewModel"/>
</UserControl.Resources>
<Grid>
<TreeView ItemsSource="{Binding CodeBookEnties}" >
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
<Setter Property="FontWeight" Value="Normal" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="FontWeight" Value="Bold" />
</Trigger>
</Style.Triggers>
</Style>
</TreeView.ItemContainerStyle>
<TreeView.Resources>
<HierarchicalDataTemplate ItemsSource="{Binding DocSetCollection}" DataType="{x:Type this:CodeBookEntity}">
<Label Content="{Binding CodeBookName}"/>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding FieldCollection}" DataType="{x:Type this:DocumentEntity}">
<Label Content="{Binding DocSetName}"/>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type this:FieldEntity}">
<Label Content="{Binding FieldName}"/>
</DataTemplate>
</TreeView.Resources>
</TreeView>
</Grid>
</UserControl>
我的viewmodel类是这样的:
namespace Helper.ViewModel
{
public class TreeNodeViewModel : ViewModelBase
{
private ObservableCollection<DocumentEntity> children;
private CodeBookEntity _codeBookEntity;
private ObservableCollection<CodeBookEntity> codeBookEntities;
//TreeNodeViewModel _parent = null;
bool _isExpanded = false;
bool _isSelected = false;
public TreeNodeViewModel()
{
Mediator.Instance.Register(
//Callback delegate, when message is seen
(Object o) =>
{
ParentNode = (CodeBookEntity)o;
}, ViewModelMessages.CodeBookCreated);
}
public override string ToString() {
return _codeBookEntity.CodeBookName;
}
public ObservableCollection<DocumentEntity> Children
{
get { return CodeBookEnties[0].DocSetCollection; }
set {
CodeBookEnties[0].DocSetCollection = value;
//OnPropertyChanged("CodeBookEnties");
}
}
public CodeBookEntity ParentNode
{
get { return _codeBookEntity; }
set {
_codeBookEntity = value;
if (CodeBookEnties == null)
{
CodeBookEnties = new ObservableCollection<CodeBookEntity>();
}
CodeBookEnties.Add(_codeBookEntity);
OnPropertyChanged("ParentNode");
}
}
public string Name {
get { return _codeBookEntity.CodeBookName; }
}
public string Description
{ get { return _codeBookEntity.CodeBookDescription; } }
#region Presentation Members
#region IsExpanded
/// <summary>
/// Gets/sets whether the TreeViewItem
/// </summary>
public bool IsExpanded
{
get { return _isExpanded; }
set
{
if (value != _isExpanded)
{
_isExpanded = value;
this.OnPropertyChanged("IsExpanded");
}
// Expand all the way up to the root.
//if (_isExpanded && _parent != null)
// _parent.IsExpanded = true;
}
}
#endregion // IsExpanded
#region IsSelected
/// <summary>
/// Gets/sets whether the TreeViewItem
/// associated with this object is selected.
/// </summary>
public bool IsSelected
{
get { return _isSelected; }
set
{
if (value != _isSelected)
{
_isSelected = value;
this.OnPropertyChanged("IsSelected");
}
}
}
#endregion // IsSelected
#region NameContainsText
public bool NameContainsText(string text)
{
if (String.IsNullOrEmpty(text) || String.IsNullOrEmpty(this.Name))
return false;
return this.Name.IndexOf(text, StringComparison.InvariantCultureIgnoreCase) > -1;
}
#endregion // NameContainsText
#endregion // Presentation Members
public bool IsLeaf {
get { return !Children.Any(); }
}
public ObservableCollection<CodeBookEntity> CodeBookEnties
{
get
{
return codeBookEntities;
}
set
{
if (codeBookEntities == null)
{
codeBookEntities = new ObservableCollection<CodeBookEntity>();
}
codeBookEntities = value;
OnPropertyChanged("CodeBookEnties");
}
}
}
}
namespace Helper.ViewModel
{
公共类TreeNodeDevice模型:ViewModelBase
{
私生子;
私有CodeBookEntity _CodeBookEntity;
私有可观察收集代码簿实体;
//TreeNodeViewModel _parent=null;
bool _isExpanded=false;
bool_isSelected=false;
公共树设备模型()
{
Mediator.Instance.Register(
//回调委托,当看到消息时
(对象o)=>
{
ParentNode=(CodeBookEntity)o;
},ViewModelMessages.CodeBookCreated);
}
公共重写字符串ToString(){
返回_codeBookEntity.CodeBookName;
}
公众观察收集儿童
{
获取{return CodeBookEnties[0].DocSetCollection;}
设置{
CodeBookEnties[0]。DocSetCollection=value;
//OnPropertyChanged(“CodeBookEnties”);
}
}
公共代码簿实体父节点
{
获取{return\u codeBookEntity;}
集合{
_codeBookEntity=值;
if(CodeBookEnties==null)
{
CodeBookEnties=新的ObservableCollection();
}
添加(\u codeBookEntity);
OnPropertyChanged(“父节点”);
}
}
公共字符串名{
获取{return\u codeBookEntity.CodeBookName;}
}
公共字符串描述
{get{return}codeBookEntity.CodeBookDescription;}
#区域演示文稿成员
#区域扩张
///
///获取/设置TreeViewItem是否
///
公共图书馆被扩展了
{
获取{return\u isExpanded;}
设置
{
如果(值!=\u扩展)
{
_isExpanded=值;
本协议项下的不动产变更(“IsExpanded”);
}
//一直展开到根。
//if(_isExpanded&&u parent!=null)
//_parent.IsExpanded=true;
}
}
#endregion//IsExpanded
#地区选举
///
///获取/设置TreeViewItem是否
///已选择与此对象关联的。
///
公选学校
{
获取{return}isSelected;}
设置
{
如果(值!=\u已选择)
{
_isSelected=值;
本协议项下的不动产变更(“IsSelected”);
}
}
}
#endregion//IsSelected
#区域名称containstext
public bool name containstext(字符串文本)
{
if(String.IsNullOrEmpty(text)| String.IsNullOrEmpty(this.Name))
返回false;
返回此.Name.IndexOf(text,StringComparison.InvariantCultureIgnoreCase)>-1;
}
#endregion//NameContainsText
#endregion//演示文稿成员
公共布尔岛{
获取{return!Children.Any();}
}
公共可观测收集代码簿
{
得到
{
返回代码簿实体;
}
设置
{
if(codeBookEntities==null)
{
codeBookEntities=新的ObservableCollection();
}
codeBookEntities=值;
OnPropertyChanged(“CodeBookEnties”);
}
}
}
}
我在我的main窗口中使用了这个usercontrol
。
每当有任何代码本添加到那里,我都会通过中介类将该信息发送到此treeviwmodel
。
基于此,它将更新其数据。能够添加多达3个级别的节点,没有任何问题
当我在调试时,选择treeview
中的任何项目,treevieview项目
将变为粗体,但IsSelected
属性不会被点击。每当我选择一个元素时,我必须根据它的选择获取它的类(代码本、文档、字段是这里的类的层次结构),以便我以后可以处理该元素。
不确定是什么错误。请帮帮我
谢谢
Rani您的代码有很多问题。这样使用绑定
将导致错误,因为您正在对象中查找IsSelected
属性,该对象被设置为树视图项
的数据上下文
,而不是在视图模型中:
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
<Setter Property="FontWeight" Value="Normal" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="FontWeight" Value="Bold" />
</Trigger>
</Style.Triggers>
</Style>
请注意,这将在绑定到视图/UserControl DataContext的数据对象中搜索名为IsSelected
的属性。。。我假设这将是您的视图模型。但我不能确定这是否真的有效。。。输入下一个问题
treevieItem.IsSelected
属性通常是绑定到数据对象的属性的数据,该属性是绑定到treevieItem
的数据。除其他原因外,这通常是因为Tr
<Setter Property="IsSelected" Value="{Binding DataContext.IsSelected, RelativeSource={
RelativeSource AncestorType={x:Type YourPrefix:YourView}}, Mode=TwoWay}" />