wpf – Silverlight树视图.无法绑定“IsExpanded”属性

wpf – Silverlight树视图.无法绑定“IsExpanded”属性,第1张

概述我有TreeView控件,我想绑定树节点的IsExpanded属性到我的DataSource项目! 但我有一个例外: System.Windows.Markup.XamlParseException occurred Message=Set property '' threw an exception. StackTrace: at System.Windows.Appli 我有TreeVIEw控件,我想绑定树节点的IsExpanded属性到我的DataSource项目!

但我有一个例外:

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;};}
解决方法 最快的方法是将TreeVIEw和TreeVIEwItem子类化,例如:

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主题概念的弱点.因此,您可以使用自定义的附加属性或行为来遍历树,并从外部设置绑定.因为树节点是按需生成的,尽管如此,您必须为尚未渲染的每个节点收听一次Expanded事件,然后在等待该节点的每个子节点的事件处理程序中设置绑定布局通行证

总结

以上是内存溢出为你收集整理的wpf – Silverlight树视图.无法绑定“IsExpanded”属性全部内容,希望文章能够帮你解决wpf – Silverlight树视图.无法绑定“IsExpanded”属性所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1000853.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存