0

I'm using avalondock version 2. I would like to know how I can programmatically bind the Title, IsSelected, etc, property. From LayoutDocument to the ViewModel. I wanted to use SetBinding but unfortunately LayoutDocument has no such method.

Update I know about this approach but unfortunately it does not suit me. I do DockingService for control Windows and DockingService have a method

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);
        }
    }

But I don't know how to bind the properties Title, IsSelected in this method

user45245
  • 845
  • 1
  • 8
  • 18

2 Answers2

0

You will have to set the instance of the ViewModel as DataContext in your View. After that you will be able to use e.g. Text="{Binding Path=YourProperty}" on the properties you want to bind..

Peter Schneider
  • 2,879
  • 1
  • 14
  • 17
0

From this article How to: Create a Binding in Code, you can use BindingOperations.SetBinding.

So your code could become like this (to bind the title as an example):

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);
    }
}
Khiro
  • 96
  • 1
  • 1
  • 5