在WPF中,我想知道如何将代码转换为附加行为,因为它遵循MVVM模式,并且更易于维护和测试。
我有以下xAML实现了一个停靠管理器:
<dxb:BarManager x:Name="MyBarManager"/>
在后面的代码中,我可以将布局保存到XML文件中:
MyBarManager.SaveLayoutToStream(...);
我也可以从XML文件加载布局:
MyBarManager.LoadLayoutFromStream(...);
为了遵循MVVM模式,我想将其转换为附加属性,因此我可以绑定到ViewModel中的字符串Test
,而不是让代码落后:
<!-- BarManager is part of the framework, it has methods to save/load layout. -->
<dxb:BarManager x:Name="MyBarManager"
attached:BarLayoutManagerAttachedProperty.DockLayoutSerialize="{Binding Test}">
到目前为止我尝试了什么
我使用了ReSharper模板&#34; dependencyProperty&#34;但是,要创建以下附加属性,似乎并不是一种连接任何调用的方法:
public class BarLayoutManagerAttachedProperty : DependencyObject
{
public static readonly DependencyProperty DockLayoutSerializeProperty = DependencyProperty.RegisterAttached(
"DockLayoutSerialize",
typeof (BarManager),
typeof (BarLayoutManagerAttachedProperty),
new PropertyMetadata(default(BarManager)));
public static void SetDockLayoutSerialize(DependencyObject element, BarManager value)
{
element.SetValue(DockLayoutSerializeProperty, value);
}
public static BarManager GetDockLayoutSerialize(DependencyObject element)
{
return (BarManager) element.GetValue(DockLayoutSerializeProperty);
}
}