Wpf 无法设置AccordionItem.IsSelected的值

Wpf 无法设置AccordionItem.IsSelected的值,wpf,accordion,wpftoolkit,expander,Wpf,Accordion,Wpftoolkit,Expander,有没有办法设置一个WPF手风琴,使它的所有项目在默认情况下都折叠起来,只有当我单击expander按钮时,它才应该展开 更新 根据模板,我意识到选择项目的是AccordionItem的IsSelected属性,问题是,当我尝试设置此属性(从按钮单击处理程序中的代码)时,我得到以下InvalidOperationException: Cannot modify the IsSelected property while item is locked. 堆栈跟踪: at System.Wind

有没有办法设置一个WPF手风琴,使它的所有项目在默认情况下都折叠起来,只有当我单击expander按钮时,它才应该展开

更新 根据模板,我意识到选择项目的是
AccordionItem
IsSelected
属性,问题是,当我尝试设置此属性(从按钮单击处理程序中的代码)时,我得到以下
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()

您可以使用
SelectedIndex
SelectedItem
手风琴控件本身的属性来控制选择哪个项目

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

<toolkit:AccordionItem Visibility="Collapsed"/>
编辑:

“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"
    ...>


您能把
i
ei
参考资料包括在内吗?这对我来说应该很神奇!终于摆脱了冗长的故事板!如果安装了Microsoft Exp.studio,是否包含Exp.blend工具包?是的,在这种情况下,您只需搜索“Expression”即可找到和程序集引用。或者,如果您在Blend中打开项目并添加任何“行为”(在“资产”下),Blend将自动添加名称空间和引用。当您发表评论时,请包含@Name引用,这样我会收到新评论的通知。我希望我能多投一次,干得好,瑞克!您只是随机地帮我找到了一个解决方案,解决了我很久以前想要实现的问题(EventTrigger类的构造函数是内部的,因此无法继承)!非常感谢。
<Window ...
    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
    xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
    ...>