如何使用动态ItemsSource将复选框添加到树视图中
本文关键字:视图 添加 复选框 何使用 动态 ItemsSource | 更新日期: 2023-09-27 18:06:03
所以我已经尝试了一大堆的例子从谷歌,但我似乎不能给我的树视图复选框....我唯一能想到的是,这是因为我的树的ItemsSource总是在变化
我正在尝试的一个例子是:带复选框的TreeView
最值得注意的是HierarchicalDataTemplate:
<HierarchicalDataTemplate
x:Key="CheckBoxItemTemplate"
ItemsSource="{Binding Children, Mode=OneTime}"
>
<StackPanel Orientation="Horizontal">
<!-- These elements are bound to a FooViewModel object. -->
<CheckBox
Focusable="False"
IsChecked="{Binding IsChecked}"
VerticalAlignment="Center"
/>
<ContentPresenter
Content="{Binding Name, Mode=OneTime}"
Margin="2,0"
/>
</StackPanel>
</HierarchicalDataTemplate>
,我将树的ItemTemplate设置为:
ItemTemplate="{StaticResource CheckBoxItemTemplate}"
但是我没有看到复选框。我认为这是因为在这个例子中,数据被绑定到Children…但在我的例子中,我总是改变itemsSource(从树移动到树,或编辑它,并将其添加回树等)…
有人知道怎么让它工作吗?
谢谢!
* 编辑 *
添加treeview itemsSource我输入
tv_master.ItemsSource = t.TreeItems;
t.TreeItems是一个List,它包含了顶级节点…
我的treeview:
<my:SpecTreeView Margin="8,8,0,12"
x:Name="tv_local"
TreeViewItem.Selected="node_Selected" HorizontalAlignment="Left" Width="304"
x:FieldModifier="private" BorderBrush="Black">
编写复选框代码的人使用的原始类:
using System.Collections.Generic;
using System.ComponentModel;
namespace TreeViewWithCheckBoxes
{
public class FooViewModel : INotifyPropertyChanged
{
#region Data
bool? _isChecked = false;
FooViewModel _parent;
#endregion // Data
#region CreateFoos
public static List<FooViewModel> CreateFoos()
{
FooViewModel root = new FooViewModel("Weapons")
{
IsInitiallySelected = true,
Children =
{
new FooViewModel("Blades")
{
Children =
{
new FooViewModel("Dagger"),
new FooViewModel("Machete"),
new FooViewModel("Sword"),
}
},
new FooViewModel("Vehicles")
{
Children =
{
new FooViewModel("Apache Helicopter"),
new FooViewModel("Submarine"),
new FooViewModel("Tank"),
}
},
new FooViewModel("Guns")
{
Children =
{
new FooViewModel("AK 47"),
new FooViewModel("Beretta"),
new FooViewModel("Uzi"),
}
},
}
};
root.Initialize();
return new List<FooViewModel> { root };
}
FooViewModel(string name)
{
this.Name = name;
this.Children = new List<FooViewModel>();
}
void Initialize()
{
foreach (FooViewModel child in this.Children)
{
child._parent = this;
child.Initialize();
}
}
#endregion // CreateFoos
#region Properties
public List<FooViewModel> Children { get; private set; }
public bool IsInitiallySelected { get; private set; }
public string Name { get; private set; }
#region IsChecked
/// <summary>
/// Gets/sets the state of the associated UI toggle (ex. CheckBox).
/// The return value is calculated based on the check state of all
/// child FooViewModels. Setting this property to true or false
/// will set all children to the same check state, and setting it
/// to any value will cause the parent to verify its check state.
/// </summary>
public bool? IsChecked
{
get { return _isChecked; }
set { this.SetIsChecked(value, true, true); }
}
void SetIsChecked(bool? value, bool updateChildren, bool updateParent)
{
if (value == _isChecked)
return;
_isChecked = value;
if (updateChildren && _isChecked.HasValue)
this.Children.ForEach(c => c.SetIsChecked(_isChecked, true, false));
if (updateParent && _parent != null)
_parent.VerifyCheckState();
this.OnPropertyChanged("IsChecked");
}
void VerifyCheckState()
{
bool? state = null;
for (int i = 0; i < this.Children.Count; ++i)
{
bool? current = this.Children[i].IsChecked;
if (i == 0)
{
state = current;
}
else if (state != current)
{
state = null;
break;
}
}
this.SetIsChecked(state, false, true);
}
#endregion // IsChecked
#endregion // Properties
#region INotifyPropertyChanged Members
void OnPropertyChanged(string prop)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(prop));
}
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}
}
My TNode Class
public class TNode : TreeViewItem{
public int Level { get; set; }
public Boolean IsCheckedStr { get; set; }
public string Section { get; set; }
public string Note { get; set; }
public Boolean Locked { get; set; }
public string ID { get; set; }
public int Hierarchy { get; set; }
public string Type { get; set; }
public Boolean HasChildren { get; set; }
public string TextBlock { get; set; }
public Boolean ShowDetails { get; set; }
public List<TNode> Dependencies { get; set; }
public TNode(string id) {
ID = id;
Dependencies = new List<TNode>();
}
我现在只想让复选框出现:(
如果你还想看什么,请告诉我
Edit: datatemplate用于数据,TreeViewItems或其子类不是数据,模板将被忽略。你应该在Visual Studio的输出窗口中看到一个关于这个的错误(这对调试任何类型的数据绑定问题都很有帮助)。
看到这没有什么问题,您可能想要发布类的代码,以及TreeView
实例声明。
ItemsSource
绑定到项目的根列表?