Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Reflection;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows.Data;
- using System.Windows.Markup;
- using System.Windows;
- using Kvint.Irradiant.Core;
- using System.Windows.Threading;
- using System.Collections.ObjectModel;
- using System.Windows.Controls;
- namespace Kvint.Irradiant.WPFHelpers
- {
- /// <summary>
- /// Класс DataBinding'а перебасывающего исключения.
- /// </summary>
- public class BindingEx : Binding
- {
- #region Private members
- private static readonly UpdateSourceExceptionFilterCallback m_ExceptionHandler = ExceptionHandler;
- private static readonly Action<BindingExpressionBase, Exception> m_RethrowHelper = RethrowHelper;
- private static readonly Func<BindingExpressionBase, DependencyObject> m_TargetAccessor;
- private static void RethrowHelper(BindingExpressionBase bex, Exception exception)
- {
- bex.UpdateTarget();
- // "Тихие" исключения надо просто глотать, они произошли в другом стеке,
- // перебрасывать их просто совершенно бессмысленно.
- if (exception is CancelException) return;
- throw NotifyUserException.Wrap(exception);
- }
- private static object ExceptionHandler(object bindingExpression, Exception exception)
- {
- var bex = bindingExpression as BindingExpression;
- if (null == bex) return null;
- var dobj = m_TargetAccessor(bex);
- dobj.Dispatcher.BeginInvoke(m_RethrowHelper, bex, exception);
- return null;
- }
- private void InitExceptionHandling()
- {
- UpdateSourceExceptionFilter = m_ExceptionHandler;
- ValidatesOnExceptions = true;
- }
- static BindingEx()
- {
- var propInfo = typeof(BindingExpressionBase).GetProperty(
- "TargetElement", BindingFlags.Instance | BindingFlags.NonPublic);
- var accessors = propInfo.GetAccessors(true);
- m_TargetAccessor = (Func<BindingExpressionBase, DependencyObject>)
- Delegate.CreateDelegate(
- typeof(Func<BindingExpressionBase, DependencyObject>), accessors[0]);
- }
- #endregion
- #region Public members
- /// <summary>
- /// Конструктор.
- /// </summary>
- public BindingEx()
- {
- InitExceptionHandling();
- }
- /// <summary>
- /// Конструктор.
- /// </summary>
- /// <param name="path">Путь к свойству.</param>
- public BindingEx(string path) : base (path)
- {
- InitExceptionHandling();
- }
- #endregion
- }
- /// <summary>
- /// Класс DataBinding'а перебасывающего исключения для стилей.
- /// </summary>
- public sealed class BindingExS : MarkupExtension
- {
- #region Private members
- private BindingEx m_Binding;
- #endregion
- #region Public members
- /// <summary>
- /// Конструктор.
- /// </summary>
- public BindingExS()
- {
- m_Binding = new BindingEx();
- }
- /// <summary>
- /// Конструктор.
- /// </summary>
- /// <param name="path">Путь к свойству.</param>
- public BindingExS(string path)
- {
- m_Binding = new BindingEx(path);
- }
- /// <summary>
- /// Gets or sets the path.
- /// </summary>
- /// <value>The path.</value>
- public PropertyPath Path
- {
- get { return m_Binding.Path; }
- set { m_Binding.Path = value; }
- }
- /// <summary>
- /// Gets or sets the mode.
- /// </summary>
- /// <value>The mode.</value>
- public BindingMode Mode
- {
- get { return m_Binding.Mode; }
- set { m_Binding.Mode = value; }
- }
- /// <summary>
- /// Gets or sets the update source trigger.
- /// </summary>
- /// <value>The update source trigger.</value>
- public UpdateSourceTrigger UpdateSourceTrigger
- {
- get { return m_Binding.UpdateSourceTrigger; }
- set { m_Binding.UpdateSourceTrigger = value; }
- }
- /// <summary>
- /// Gets the validation rules.
- /// </summary>
- /// <value>The validation rules.</value>
- public Collection<ValidationRule> ValidationRules
- {
- get { return m_Binding.ValidationRules; }
- }
- /// <summary>
- /// When implemented in a derived class, returns an object that is set as the value of the target property for this markup extension.
- /// </summary>
- /// <param name="serviceProvider">Object that can provide services for the markup extension.</param>
- /// <returns>
- /// The object value to set on the property where the extension is applied.
- /// </returns>
- public sealed override object ProvideValue(IServiceProvider serviceProvider)
- {
- return m_Binding;
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement