我现在正在寻找解决此问题的方法: 我想开发一个树形视图,填充了复选框,但我无法从中获取已检查的项目。
帮助程序类如下所示:
class ArchfabTreeViewItemViewHelper : System.Windows.DependencyObject
{
public string Key { get; set; }
public string Value { get; set; }
public bool IsChecked { get; set; }
public static readonly System.Windows.DependencyProperty IsCheckedProperty = System.Windows.DependencyProperty.RegisterAttached("IsChecked", typeof(bool?), typeof(ArchfabTreeViewItemViewHelper), new System.Windows.PropertyMetadata(false, new System.Windows.PropertyChangedCallback(OnIsCheckedPropertyChanged)));
public static readonly System.Windows.DependencyProperty ParentProperty = System.Windows.DependencyProperty.RegisterAttached("Parent", typeof(object), typeof(ArchfabTreeViewItemViewHelper));
public ArchfabTreeViewItemViewHelper() { }
public ArchfabTreeViewItemViewHelper(string tag, string value)
{
this.Key = tag;
this.Value = value;
//this.IsChecked = false;
}
//methods
private static void OnIsCheckedPropertyChanged(System.Windows.DependencyObject d, System.Windows.DependencyPropertyChangedEventArgs e)
{
ArchfabTreeViewItemViewHelper helper = (ArchfabTreeViewItemViewHelper)d;
ArchfabTreeViewItemViewHelper.SetIsChecked(d.GetValue(ArchfabTreeViewItemViewHelper.ParentProperty) as System.Windows.DependencyObject, true);
}
我想,我在生成HierarchialDataTemplate时跳过了一些重要的东西。 这是模板:
class ArchfabHierarchialDataTemplate : System.Windows.HierarchicalDataTemplate
{
private static ArchfabHierarchialDataTemplate NewCheckBoxTemplate()
{
ArchfabHierarchialDataTemplate resultTemplate = new ArchfabHierarchialDataTemplate();
//stackpanel
System.Windows.FrameworkElementFactory stackpanel = new System.Windows.FrameworkElementFactory(typeof(ArchfabContainer)) { Name = "Parent" };
stackpanel.SetValue(System.Windows.FrameworkElement.HeightProperty, 23.00);
stackpanel.SetValue(System.Windows.FrameworkElement.WidthProperty, 300.00);
stackpanel.SetValue(System.Windows.FrameworkElement.MarginProperty, new System.Windows.Thickness(2));
stackpanel.SetValue(ArchfabContainer.BackgroundProperty, new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.LightSteelBlue));
//checkbox
//fields and properties
System.Windows.FrameworkElementFactory checkbox = new System.Windows.FrameworkElementFactory(typeof(ArchfabCheckBox)) { Name = "checkBox" };
checkbox.SetValue(System.Windows.FrameworkElement.HeightProperty, 23.00);
checkbox.SetValue(System.Windows.FrameworkElement.WidthProperty, 300.00);
checkbox.SetValue(System.Windows.FrameworkElement.MarginProperty, new System.Windows.Thickness(2));
checkbox.SetBinding(ArchfabCheckBox.ContentProperty, new System.Windows.Data.Binding() { Path = new System.Windows.PropertyPath("Value") });
checkbox.SetBinding(ArchfabCheckBox.TagProperty, new System.Windows.Data.Binding() { Path = new System.Windows.PropertyPath("Tag") });
checkbox.SetBinding(ArchfabCheckBox.IsCheckedProperty, new System.Windows.Data.Binding() { Path = new System.Windows.PropertyPath("local:ArchfabTreeViewItemViewHelper.IsChecked"), Mode = System.Windows.Data.BindingMode.TwoWay , UpdateSourceTrigger=System.Windows.Data.UpdateSourceTrigger.PropertyChanged});
stackpanel.AppendChild(checkbox);
resultTemplate.VisualTree = stackpanel;
return resultTemplate;
}
}
搜索已检查项目的方法是递归方法调用,在树视图层次结构中上下移动所有不同级别
public static List<KeyValueObject> FindSelectedElements(ArchfabTreeView treeView)
{
List<KeyValueObject> result = new List<KeyValueObject>();
ArchfabTreeViewItem temp = null;
foreach (object item in treeView.ItemsSource)
{
try
{
//if this is possible, you have to go a level deeper
temp = (ArchfabTreeViewItem)item;
result = FindSelectedElements(result, temp);
}
catch {
//if catch is thrown, we have a Kv-Object here --> now check for isChecked
ArchfabTreeViewItemViewHelper elem = (ArchfabTreeViewItemViewHelper)item;
if (elem != null)
{
if (elem.IsChecked)
{
result.Add(new KeyValueObject(elem.Key, elem.Value));
}
}
}
}
return result;
}
internal static List<KeyValueObject> FindSelectedElements(List<KeyValueObject> listKV, ArchfabTreeViewItem treeViewItem)
{
List<KeyValueObject> result = listKV;
ArchfabTreeViewItem temp = null;
foreach (object item in treeViewItem.ItemsSource)
{
try
{
//if this is possible, you have to go a level deeper
temp = (ArchfabTreeViewItem)item;
result = FindSelectedElements(result, temp);
}
catch
{
//if catch is thrown, we have a Kv-Object here --> now check for isChecked
ArchfabTreeViewItemViewHelper elem = (ArchfabTreeViewItemViewHelper)item;
if (elem != null)
{
if (elem.IsChecked == true)
{
result.Add(new KeyValueObject(elem.Key,elem.Value));
}
}
}
}
return result;
}
我已经检查过有趣的文章,由Josh Smith撰写有关树视图和复选框的文章,它很有用......但是我找不到解决方案 https://www.codeproject.com/kb/wpf/treeviewwithcheckboxes.aspx?msg=3042540
最后,没有抛出异常或与此平行的东西,但我的助手类的“IsChecked”属性对于我选择的任何对象都没有改变:/