Example #1
0
        /// <summary>
        /// 必要ならGUIスレッド上でPropertyChangedを呼び出します。
        /// </summary>
        public static void CallPropertyChanged(PropertyChangedEventHandler handler,
                                               object sender,
                                               PropertyChangedEventArgs e)
        {
            if (handler == null)
            {
                return;
            }

            WPFUtil.UIProcess(() =>
            {
                try
                {
                    handler(sender, e);
                }
                catch (Exception ex)
                {
                    Log.ErrorException(ex,
                                       "PropertyChangedの呼び出しに失敗しました。");
                }
            });

            // 個々のDelegate単位で呼び出すスレッドを変更します。

            /*foreach (PropertyChangedEventHandler child in
             *       handler.GetInvocationList())
             * {
             *  var target = child.Target as DispatcherObject;
             *
             *  try
             *  {
             *      // 必要があれば指定のスレッド上で実行します。
             *      if (target != null && !target.Dispatcher.CheckAccess())
             *      {
             *          target.Dispatcher.BeginInvoke(
             *              child,
             *              sender, e);
             *      }
             *      else
             *      {
             *          child(sender, e);
             *      }
             *  }
             *  catch (Exception ex)
             *  {
             *      Log.ErrorException(ex,
             *          "PropertyChangedの呼び出しに失敗しました。");
             *  }
             * }*/
        }
Example #2
0
        /// <summary>
        /// マウス位置を中心にダイアログを開きます。
        /// </summary>
        public static bool?ShowDialogCenterMouse(this Window dialog)
        {
            if (dialog == null)
            {
                throw new ArgumentNullException("dialog");
            }

            // WPFではコントロールのサイズは実際にそれを開いた後に決まります。
            dialog.Loaded += (sender, e) =>
            {
                var p         = WPFUtil.GetMousePosition(dialog);
                var screenPos = dialog.PointToScreen(p);

                dialog.WindowStartupLocation = WindowStartupLocation.Manual;
                dialog.Left = screenPos.X - (dialog.ActualWidth / 2);
                dialog.Top  = screenPos.Y - (dialog.ActualHeight / 2);
                dialog.AdjustInDisplay();
            };

            return(dialog.ShowDialog());
        }