gpt4 book ai didi

wpf - Silverlight TreeView 。无法绑定(bind) "IsExpanded"属性

转载 作者:行者123 更新时间:2023-12-04 03:34:50 24 4
gpt4 key购买 nike

我有 TreeView控制和我想绑定(bind)树节点的IsExpanded属性(property)归我DataSource项目!

但我有一个异常(exception):

System.Windows.Markup.XamlParseException occurred
Message=Set property '' threw an exception.

StackTrace:
at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
at SilverlightTree.BSTreeView.InitializeComponent()
at SilverlightTree.BSTreeView..ctor()
InnerException: System.NotSupportedException
Message=Cannot set read-only property ''.
StackTrace:
at MS.Internal.XamlMemberInfo.SetValue(Object target, Object value)
at MS.Internal.XamlManagedRuntimeRPInvokes.SetValue(XamlTypeToken inType, XamlQualifiedObject& inObj, XamlPropertyToken inProperty, XamlQualifiedObject& inValue)
InnerException:

内部异常:
{System.NotSupportedException: Cannot set read-only property ''.

XAML:
<Grid x:Name="LayoutRoot">
<controls:TreeView Name="treeView" SelectedItemChanged="treeView_SelectedItemChanged"
Style="{Binding TreeViewConnectingLines}" BorderBrush="{x:Null}">
<controls:TreeView.ItemTemplate>
<toolkit:HierarchicalDataTemplate ItemsSource="{Binding Children}">
<StackPanel Orientation="Horizontal" Background="Transparent">
<toolkitDrag:ContextMenuService.ContextMenu>
<toolkitDrag:ContextMenu Loaded="ContextMenu_Loaded"
Opened="ContextMenu_Opened"/>
</toolkitDrag:ContextMenuService.ContextMenu>
<Image Source="{Binding Path=Type.Icon}" Width="20" Height="20" />
<TextBlock Text="{Binding Path=FullDescription}" Height="20"
TextAlignment="Center" HorizontalAlignment="Center" />
</StackPanel>
</toolkit:HierarchicalDataTemplate>
</controls:TreeView.ItemTemplate>
<controls:TreeView.ItemContainerStyle>
<Style TargetType="controls:TreeViewItem">
<Setter Property="IsExpanded" Value="{Binding IsExpanded}"></Setter>
</Style>
</controls:TreeView.ItemContainerStyle>
</controls:TreeView>
</Grid>

和数据项:
public interface INode
{
NodeType Type { get; set; }
bool IsSelected { get; set; }
bool IsExpanded { get; set; }
List<INode> Children{get;set;};
}

最佳答案

最快的方法是继承 TreeViewTreeViewItem , 例如:

public class BindableTreeViewItem : TreeViewItem
{
protected override DependencyObject GetContainerForItemOverride()
{
var itm = new BindableTreeViewItem();
itm.SetBinding(TreeViewItem.IsExpandedProperty, new Binding("IsExpanded") { Mode = BindingMode.TwoWay });

return itm;
}
}

public class BindableTreeView : TreeView
{
protected override DependencyObject GetContainerForItemOverride()
{
var itm = new BindableTreeViewItem();
itm.SetBinding(TreeViewItem.IsExpandedProperty, new Binding("IsExpanded") { Mode = BindingMode.TwoWay });

return itm;
}
}

不幸的是,您将失去 TreeView 的默认主题。当你做子类化时。这是 Silverlight 主题概念的一个弱点。因此,您可以选择使用自定义附加属性或遍历树并从外部设置绑定(bind)的行为。由于树节点是按需延迟创建的,因此您必须收听 Expanded为尚未呈现的每个节点事件一次,然后在等待布局传递后为其每个子节点设置该事件处理程序中的绑定(bind)。

关于wpf - Silverlight TreeView 。无法绑定(bind) "IsExpanded"属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3316597/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com