Implement dirty notification on avalon doc tab + caliburn micro

I got a problem to solve when trying to implement the VS-like dirty notification in my WPF app,

I realized the way I opened the AvalonDockTab in my previous post is not very friendly to make this happen.

When ViewModel changed the displayname of Screen after the tab openning, it’s very hard the notify view to refresh it.

Using code to set databinding is a much better solution.


private DocumentContent CreateTab(object view, Screen currentViewModel)
{
	var documentContent = new DocumentContent
							  {
								  DataContext =  (currentViewModel),
								  Content = view,
//                Title = currentViewModel.DisplayName, // should set it via databinding, if vm changed it later, how to refresh title?

							  };

	BindTabTitle(documentContent);

//            documentContent.SetBinding(TitleProperty, new Binding("DisplayName"));  //why this does not work?
	return documentContent;
}

static void BindTabTitle(DocumentContent tab)
{
	DependencyProperty textProp = DocumentContent.TitleProperty;
	if (!BindingOperations.IsDataBound(tab, textProp))
	{
		Binding b = new Binding("DisplayName");
		BindingOperations.SetBinding(tab, textProp, b);
	}
}
//... in view model
        private string _name;
        public string Name
        {
            get { return _name; }
            set {
                _name = value;
                DisplayName = "Product details: " + _name;

                if (_name != _product.Name) DisplayName += "*";
            }
        }
	private void SetOriginalData()
	{
		Name = _product.Name;
		Description = _product.Description;
	}

	public void Save()
	{
		_product.Name = Name;
		_product.Description = Description;

		try
		{
			_productCrudTask.Save(_product);

			_messageBox.Show("Product " + Name + " saved successfully.");

			SetOriginalData(); // to reset dirty flag, still looking for a better way.

		}
		catch (DataValidationException e)
		{
			_messageBox.Show("Error! " + e.Message);
		}
	}

	public void Cancel()
	{
		SetOriginalData();
	}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s