0

WPF/XAML に TextBox のサブクラスがあり、他のすべての TextBox インスタンスと同じスタイルを適用したいと考えています。次のスタイルが定義されています

<Style TargetType="TextBox" x:Key="basicTextBox" >
    <Setter Property="Controls:TextBoxBehaviours.UpdateWhenEnterPressed" Value="True"/>
    <Setter Property="Controls:TextBoxBehaviours.SelectAllWhenEnterPressed" Value="True"/>
    <Setter Property="Controls:TextBoxBehaviours.SelectAllOnFocus" Value="True"/>
</Style>

およびこれらを実装するためのクラス TextBoxBehaviours

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Reactive.Linq;

namespace Weingartner.Controls
{
    public static class TextBoxBehaviours
    {

        static TextBoxBehaviours()
        {}

        #region Binding Support
        private static Dictionary<Tuple<TextBox,string>, IDisposable> Bindings 
            = new Dictionary<Tuple<TextBox,string>, IDisposable>();

        private static void Bind(TextBox tb, string key, IDisposable d)
        {
            Bindings[Tuple.Create(tb, key)] = d;
        }

        private static void UnBind(TextBox tb, string key)
        {
            var t = Tuple.Create(tb, key);
            if (Bindings.ContainsKey(t))
            {
                var d = Bindings[t];
                Bindings.Remove(t);
                d.Dispose();
            }
        }
        #endregion

        static UIPropertyMetadata CreateMeta<T>(bool defaultValue, Action<T,DependencyPropertyChangedEventArgs> fn)
        where T : DependencyObject
        {
            return new UIPropertyMetadata(defaultValue, (o, e) =>{
                var t = o as T;
                if (t == null)
                {
                    return;
                }
                fn(t, e);
            });
        }

        #region Update When Enter Pressed
        public static readonly DependencyProperty 
            UpdateWhenEnterPressedProperty = 
            DependencyProperty.RegisterAttached
            ( "UpdateWhenEnterPressed"
            , typeof(bool)
            , typeof(TextBoxBehaviours)
            , CreateMeta<TextBox>(false, SetupUpdateOnEnterPressed));

        public static void 
            SetUpdateWhenEnterPressed
            ( TextBox dp
            , bool value)
        {
            dp.SetValue(UpdateWhenEnterPressedProperty, value);
        }

        public static bool 
            GetUpdateWhenEnterPressed
            ( TextBox dp)
        {
            return (bool)dp.GetValue(UpdateWhenEnterPressedProperty);
        }

        private static void 
            SetupUpdateOnEnterPressed
            ( TextBox element
            , DependencyPropertyChangedEventArgs e)
        {
            if ((bool)e.NewValue)
            {
                var sub = element
                    .PreviewKeyDownObserver()
                    .Where(x=>x.EventArgs.Key==Key.Enter)
                    .Subscribe(x=>DoUpdateSource(element));

                Bind(element, "KeyEnter", sub);
            }else{
                UnBind(element, "KeyEnter");
            }

        }

        static void DoUpdateSource(TextBox source)
        {
            BindingExpression binding = BindingOperations.GetBindingExpression(source, TextBox.TextProperty);

            if (binding != null)
            {
                binding.UpdateSource();
            }
        }
        #endregion
        #region SelectAll When Enter Pressed
        public static readonly DependencyProperty 
            SelectAllWhenEnterPressedProperty = 
            DependencyProperty.RegisterAttached
            ( "SelectAllWhenEnterPressed"
            , typeof(bool)
            , typeof(TextBoxBehaviours)
            , CreateMeta<TextBox>(false, SetupSelectAllOnEnterPressed));

        public static void 
            SetSelectAllWhenEnterPressed
            ( TextBox dp
            , bool value)
        {
            dp.SetValue(SelectAllWhenEnterPressedProperty, value);
        }

        public static bool 
            GetSelectAllWhenEnterPressed
            ( TextBox dp)
        {
            return (bool)dp.GetValue(SelectAllWhenEnterPressedProperty);
        }

        private static void 
            SetupSelectAllOnEnterPressed
            ( TextBox element
            , DependencyPropertyChangedEventArgs e)
        {
            if ((bool)e.NewValue)
            {
                var sub = element
                    .PreviewKeyDownObserver()
                    .Where(x=>x.EventArgs.Key==Key.Enter)
                    .Subscribe(x=>element.SelectAll());

                Bind(element, "KeyEnterSelectAll", sub);
            }else{
                UnBind(element, "KeyEnterSelectAll");
            }

        }

        #endregion
        #region Select All On Focus
        public static readonly DependencyProperty 
            SelectAllOnFocusProperty = 
            DependencyProperty.RegisterAttached
            ( "SelectAllOnFocus"
            , typeof(bool)
            , typeof(TextBoxBehaviours)
            , CreateMeta<TextBox>(false, SetupSelectAllOnFocus));


        public static void 
            SetSelectAllOnFocus
            ( TextBox dp
            , bool value)
        {
            dp.SetValue(SelectAllOnFocusProperty, value);
        }

        public static bool 
            GetSelectAllOnFocus
            ( TextBox dp)
        {
            return (bool)dp.GetValue(SelectAllOnFocusProperty);
        }

        private static void 
            SetupSelectAllOnFocus
            ( TextBox element
            , DependencyPropertyChangedEventArgs e)
        {
            if ((bool)e.NewValue)
            {
                Bind(element, "Focus", element.GotFocusObserver().Subscribe(x => element.SelectAll()));
            }else{
                UnBind(element, "Focus");
            }
        }
        #endregion

    }
}

私が行ってきたXAMLファイルで

<Style TargetType="TextBox" BasedOn="{StaticResource basicTextBox}"/>

そして、次のすべての TextBoxes は、私が望む動作を取得します。私のサブクラスも TextBox クラスであることを考えると、これらも動作すると思いましたが、そうではありません。スタイルを拡張して明示的にしようとしたので

<Style TargetType="TextBox" BasedOn="{StaticResource basicTextBox}"/>

<Style TargetType="Controls:EditForLength">
    <Setter Property="Controls:TextBoxBehaviours.UpdateWhenEnterPressed" Value="True"/>
    <Setter Property="Controls:TextBoxBehaviours.SelectAllWhenEnterPressed" Value="True"/>
    <Setter Property="Controls:TextBoxBehaviours.SelectAllOnFocus" Value="True"/>

</Style>

しかし、キーを教えてくれるエラーが表示されますEditForLength

System.ArgumentException occurred
  HResult=-2147024809
  Message=Item has already been added. Key in dictionary: 'Weingartner.Controls.EditForLength'  Key being added: 'Weingartner.Controls.EditForLength'
  Source=mscorlib
  StackTrace:
       at System.Collections.Hashtable.Insert(Object key, Object nvalue, Boolean add)
  InnerException: 

したがって、要約すると、親クラスのスタイルを介してインストールされた動作は子クラスのインスタンスに伝播しませんが、子クラスを対象とする別のスタイルを追加しようとすると、辞書エラーが発生します。

EditForLengthコントロールのインスタンスで動作を明示的に試してみたので、正常に動作することに注意してください。スタイリングを介して動作をインストールすることはできません。

アップデート

スタイル宣言を除くすべての重要なビットがコメントアウトされている場合でもエラーが発生することを発見しました

        <!--<Style TargetType="TextBox" BasedOn="{StaticResource basicTextBox}"/>-->

        <Style TargetType="Controls:EditForLength">
            <!--
            <Setter Property="Controls:TextBoxBehaviours.UpdateWhenEnterPressed" Value="True"/>
            <Setter Property="Controls:TextBoxBehaviours.SelectAllWhenEnterPressed" Value="True"/>
            <Setter Property="Controls:TextBoxBehaviours.SelectAllOnFocus" Value="True"/>
             -->

        </Style>
4

1 に答える 1