![](/img/trans.png)
[英]How can I bind to the IsSelected property of a DataGridRow in a sort-safe manner?
[英]avalondock how I can programmatically bind the Title, IsSelected, etc, property
我正在使用 avalondock 版本 2。我想知道如何以编程方式绑定 Title、IsSelected 等属性。 从 LayoutDocument 到 ViewModel。 我想使用 SetBinding 但不幸的是 LayoutDocument 没有这样的方法。
更新我知道这种方法,但不幸的是它不适合我。 我做 DockingService 来控制 Windows 和 DockingService 有一个方法
public void ShowDocumentWindow<TViewModel>() where TViewModel : DocumentItemViewModel
{
var viewModel = this.CreateViewModel(typeof(TViewModel));
var view = this.CreateView(viewModel);
var documentPane = this.dockingManager.Layout.Descendents().OfType<LayoutDocumentPane>().FirstOrDefault();
if (documentPane != null)
{
var layoutDocument = new LayoutDocument
{
Content = view
};
documentPane.Children.Add(layoutDocument);
}
}
但是我不知道如何在这个方法中绑定属性Title、IsSelected
您必须在您的视图中将 ViewModel 的实例设置为 DataContext。 之后,您将能够在要绑定的属性上使用例如Text="{Binding Path=YourProperty}"
..
从这篇文章如何:在代码中创建绑定,您可以使用BindingOperations.SetBinding
。
所以你的代码可以变成这样(以绑定标题为例):
public void ShowDocumentWindow<TViewModel>() where TViewModel : DocumentItemViewModel
{
var viewModel = this.CreateViewModel(typeof(TViewModel));
var view = this.CreateView(viewModel);
var documentPane = this.dockingManager.Layout.Descendents().OfType<LayoutDocumentPane>().FirstOrDefault();
if (documentPane != null)
{
var layoutDocument = new LayoutDocument
{
Content = view
};
Binding binding = new Binding("SomeProperty"); //viewModel.SomeProperty
binding.Source = viewModel;
BindingOperations.SetBinding(layoutDocument, LayoutDocument.TitleProperty, binding);
documentPane.Children.Add(layoutDocument);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.