【问题标题】:Unable to set value of AccordionItem.IsSelected无法设置 AccordionItem.IsSelected 的值
【发布时间】:2011-06-08 05:15:51
【问题描述】:

有什么方法可以设置 WPF 手风琴,使其所有项目默认折叠,只有当我单击展开器按钮时它才会展开?

更新 根据模板,我意识到选择该项目的是AccordionItemIsSelected 属性,问题是,当我尝试设置此属性时(来自按钮单击处理程序中的代码)我得到以下InvalidOperationException

Cannot modify the IsSelected property while item is locked.

堆栈跟踪:

  at System.Windows.Controls.AccordionItem.OnIsSelectedPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) in C:\dd\WPF_1\src\wpf\src\ControlsPack\WPFToolkit\Layout\Accordion\System\Windows\Controls\AccordionItem.cs:line 315
   at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
   at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
   at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
   at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
   at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
   at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
   at System.Windows.Controls.AccordionItem.set_IsSelected(Boolean value) in C:\dd\WPF_1\src\wpf\src\ControlsPack\WPFToolkit\Layout\Accordion\System\Windows\Controls\AccordionItem.cs:line 284
   at WpfSampleApplication.MainWindow.btn_Click(Object sender, RoutedEventArgs e) in d:\users\shimmy\documents\visual studio 2010\Projects\WpfSampleApplication\MainWindow.xaml.vb:line 9
   at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
   at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
   at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
   at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
   at System.Windows.Controls.Primitives.ButtonBase.OnClick()
   at System.Windows.Controls.Button.OnClick()
   at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
   at System.Windows.UIElement.OnMouseLeftButtonUpThunk(Object sender, MouseButtonEventArgs e)
   at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
   at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
   at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
   at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
   at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
   at System.Windows.UIElement.OnMouseUpThunk(Object sender, MouseButtonEventArgs e)
   at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
   at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
   at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
   at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
   at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
   at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
   at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
   at System.Windows.Input.InputManager.ProcessStagingArea()
   at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
   at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
   at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
   at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
   at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
   at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
   at System.Windows.Threading.Dispatcher.Run()
   at System.Windows.Application.RunDispatcher(Object ignore)
   at System.Windows.Application.RunInternal(Window window)
   at System.Windows.Application.Run(Window window)
   at System.Windows.Application.Run()
   at WpfSampleApplication.Application.Main() in d:\users\shimmy\documents\visual studio 2010\Projects\WpfSampleApplication\obj\x86\Debug\Application.g.vb:line 64
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

【问题讨论】:

    标签: wpf accordion wpftoolkit expander


    【解决方案1】:

    您可以使用Accordion 控件本身的SelectedIndexSelectedItem 属性来控制选择哪个项目。

    您可以将不可见的AccordionItem 作为第一个项目,以将所有项目最初显示为折叠状态:

    <toolkit:AccordionItem Visibility="Collapsed"/>
    

    要打开特定项目,请在 Loaded 处理程序中设置 SelectedIndex 属性。或者,如果您使用行为,则可以在 XAML 中执行所有操作:

    <toolkit:Accordion>
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Loaded">
                <ei:ChangePropertyAction PropertyName="SelectedIndex" Value="2"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    

    编辑:

    “xmlns:i”命名空间是指System.Windows.Interactivity,它是 WPF4 的一部分。 “xmlns:ei”命名空间指的是Microsoft.Expression.Interactions,它是以下内容的一部分:

    添加对项目的引用后,您可以使用它来设置命名空间:

    <Window ...
        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
        ...>
    

    【讨论】:

    • 能否请附上iei 参考?这对我来说听起来很神奇!终于摆脱了冗长的废话故事板!
    • 是Exp。如果 Microsoft exp 包含混合套件。工作室安装了吗?
    • 是的,在这种情况下,您只需搜索“表达式”即可找到程序集参考。或者,如果您在 Blend 中打开项目并添加任何“行为”(在“资产”下),Blend 将自动添加命名空间和引用。
    • 当您发表评论时,请附上@Name 引用,以便我收到新评论的通知。我希望我能多次投票,做得好瑞克!你只是随机地帮我找到了一个解决方案来解决我很久以前想要实现的东西(EventTrigger 类的构造器是内部的,因此不能被继承)!谢谢!!!
    猜你喜欢
    • 2017-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-05
    • 2014-12-25
    • 1970-01-01
    • 2021-05-30
    • 1970-01-01
    相关资源
    最近更新 更多