所以我尝试了谷歌的一大堆例子,但我似乎无法给出我的树视图复选框....我唯一能想到的是因为我的树的ItemsSource总是在改变
我尝试的一个例子是:TreeView with Checkboxes
最值得注意的是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 ..
我的树视图:
<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
}
}
我的TNode课程
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>();
}
我只是想让复选框暂时出现:(
如果您还有其他想看的话,请告诉我
答案 0 :(得分:1)
编辑: DataTemplates用于数据,TreeViewItems或其子类是 not 数据,模板将被忽略。您应该在Visual Studio的Output-window中看到有关此错误(这有助于调试任何类型的数据绑定问题)。
看到没有错,您可能想要发布类的代码和TreeView
实例声明。
常见错误是缺少通知接口。另外:您是否将TreeView本身的ItemsSource
绑定到项目的根列表?