Advertisement
Guest User

BindingEx

a guest
Jan 10th, 2013
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.40 KB | None | 0 0
  1. using System;
  2. using System.Reflection;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Windows.Data;
  7. using System.Windows.Markup;
  8. using System.Windows;
  9. using Kvint.Irradiant.Core;
  10. using System.Windows.Threading;
  11. using System.Collections.ObjectModel;
  12. using System.Windows.Controls;
  13.  
  14. namespace Kvint.Irradiant.WPFHelpers
  15. {
  16.     /// <summary>
  17.     /// Класс DataBinding'а перебасывающего исключения.
  18.     /// </summary>
  19.     public class BindingEx : Binding
  20.     {
  21.         #region Private members
  22.  
  23.         private static readonly UpdateSourceExceptionFilterCallback m_ExceptionHandler = ExceptionHandler;
  24.         private static readonly Action<BindingExpressionBase, Exception> m_RethrowHelper = RethrowHelper;
  25.         private static readonly Func<BindingExpressionBase, DependencyObject> m_TargetAccessor;
  26.        
  27.         private static void RethrowHelper(BindingExpressionBase bex, Exception exception)
  28.         {
  29.             bex.UpdateTarget();
  30.  
  31.             // "Тихие" исключения надо просто глотать, они произошли в другом стеке,
  32.             // перебрасывать их просто совершенно бессмысленно.
  33.  
  34.             if (exception is CancelException) return;
  35.             throw NotifyUserException.Wrap(exception);
  36.         }
  37.  
  38.         private static object ExceptionHandler(object bindingExpression, Exception exception)
  39.         {
  40.             var bex = bindingExpression as BindingExpression;
  41.             if (null == bex) return null;
  42.             var dobj = m_TargetAccessor(bex);
  43.             dobj.Dispatcher.BeginInvoke(m_RethrowHelper, bex, exception);
  44.             return null;
  45.         }
  46.  
  47.         private void InitExceptionHandling()
  48.         {
  49.             UpdateSourceExceptionFilter = m_ExceptionHandler;
  50.             ValidatesOnExceptions = true;
  51.         }
  52.  
  53.         static BindingEx()
  54.         {
  55.             var propInfo = typeof(BindingExpressionBase).GetProperty(
  56.                 "TargetElement", BindingFlags.Instance | BindingFlags.NonPublic);
  57.             var accessors = propInfo.GetAccessors(true);
  58.             m_TargetAccessor = (Func<BindingExpressionBase, DependencyObject>)
  59.                 Delegate.CreateDelegate(
  60.                     typeof(Func<BindingExpressionBase, DependencyObject>), accessors[0]);
  61.         }
  62.        
  63.         #endregion
  64.  
  65.         #region Public members
  66.  
  67.         /// <summary>
  68.         /// Конструктор.
  69.         /// </summary>
  70.         public BindingEx()
  71.         {
  72.             InitExceptionHandling();
  73.         }
  74.  
  75.         /// <summary>
  76.         /// Конструктор.
  77.         /// </summary>
  78.         /// <param name="path">Путь к свойству.</param>
  79.         public BindingEx(string path) : base (path)
  80.         {
  81.             InitExceptionHandling();
  82.         }
  83.  
  84.         #endregion
  85.     }
  86.  
  87.     /// <summary>
  88.     /// Класс DataBinding'а перебасывающего исключения для стилей.
  89.     /// </summary>
  90.     public sealed class BindingExS : MarkupExtension
  91.     {
  92.         #region Private members
  93.  
  94.         private BindingEx m_Binding;
  95.  
  96.         #endregion
  97.  
  98.         #region Public members
  99.  
  100.         /// <summary>
  101.         /// Конструктор.
  102.         /// </summary>
  103.         public BindingExS()
  104.         {
  105.             m_Binding = new BindingEx();
  106.         }
  107.  
  108.         /// <summary>
  109.         /// Конструктор.
  110.         /// </summary>
  111.         /// <param name="path">Путь к свойству.</param>
  112.         public BindingExS(string path)
  113.         {
  114.             m_Binding = new BindingEx(path);
  115.         }
  116.  
  117.         /// <summary>
  118.         /// Gets or sets the path.
  119.         /// </summary>
  120.         /// <value>The path.</value>
  121.         public PropertyPath Path
  122.         {
  123.             get { return m_Binding.Path; }
  124.             set { m_Binding.Path = value; }
  125.         }
  126.  
  127.         /// <summary>
  128.         /// Gets or sets the mode.
  129.         /// </summary>
  130.         /// <value>The mode.</value>
  131.         public BindingMode Mode
  132.         {
  133.             get { return m_Binding.Mode; }
  134.             set { m_Binding.Mode = value; }
  135.         }
  136.  
  137.         /// <summary>
  138.         /// Gets or sets the update source trigger.
  139.         /// </summary>
  140.         /// <value>The update source trigger.</value>
  141.         public UpdateSourceTrigger UpdateSourceTrigger
  142.         {
  143.             get { return m_Binding.UpdateSourceTrigger; }
  144.             set { m_Binding.UpdateSourceTrigger = value; }
  145.         }
  146.  
  147.         /// <summary>
  148.         /// Gets the validation rules.
  149.         /// </summary>
  150.         /// <value>The validation rules.</value>
  151.         public Collection<ValidationRule> ValidationRules
  152.         {
  153.             get { return m_Binding.ValidationRules; }
  154.         }
  155.  
  156.         /// <summary>
  157.         /// When implemented in a derived class, returns an object that is set as the value of the target property for this markup extension.
  158.         /// </summary>
  159.         /// <param name="serviceProvider">Object that can provide services for the markup extension.</param>
  160.         /// <returns>
  161.         /// The object value to set on the property where the extension is applied.
  162.         /// </returns>
  163.         public sealed override object ProvideValue(IServiceProvider serviceProvider)
  164.         {
  165.             return m_Binding;
  166.         }
  167.  
  168.         #endregion
  169.     }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement