我对这个问题的最佳方法感到有些困惑。我已经从第三方控件中获取,因此我可以创建一个可以在我的WPF MVVM应用程序中绑定的属性。我无法绑定它,因为它不是依赖属性。如果我要作为依赖属性实现,我可以在集合中调用RestoreLayout(value);
,但我不会在get感觉错误的情况下使用this.GetValue(LayoutXmlProperty);
获取值。下面我有什么显然不起作用,因为我不能将我的视图中的控件的LayoutXml属性绑定到我的视图模型中的相关属性。
public class WorkspaceLayoutControl : DockLayoutManager
{
public string LayoutXml
{
get { return GetLayoutAsXml(); }
set { RestoreLayout(value); }
}
private void RestoreLayout(string xml)
{
if (xml != String.Empty)
{
MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter(stream);
writer.Write(xml);
writer.Flush();
stream.Position = 0;
RestoreLayoutFromStream(stream);
}
}
private string GetLayoutAsXml()
{
var stream = new MemoryStream();
SaveLayoutToStream(stream);
var writer = new StreamWriter(stream, Encoding.UTF8);
var buffer = new byte[stream.Length];
stream.Read(buffer, 0, (int)stream.Length);
return Encoding.UTF8.GetString(buffer);
}
}
答案 0 :(得分:0)
要扩展我的评论,您可以为此目的注册PropertyChangedCallback
。这样你的getter和setter就严格处理依赖属性连接,回调就是处理所需的额外逻辑。
public static readonly DependencyProperty LayoutXmlProperty = DependencyProperty.Register(
"LayoutXml", typeof(string), typeof(WorkspaceLayoutControl),
new FrameworkPropertyMetadata(defaultValue,
new PropertyChangedCallback(OnLayoutXmlChanged));
public string LayoutXml
{
get { return (string) GetValue(LayoutXmlProperty); }
set { SetValue(LayoutXmlProperty, value); }
}
private static void OnLayoutXmlChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var layoutControl = d as WorkspaceLayoutControl;
if (layoutControl != null)
{
layoutControl.RestoreLayout(layoutControl.LayoutXml);
}
}
MSDN文章更深入地介绍:http://msdn.microsoft.com/en-us/library/ms745795(v=vs.110).aspx