コントロールに依存関係プロパティを追加する
-
03-07-2019 - |
質問
Infragistics XamDateTimeEditorコントロールを使用していますが、コントロールにフォーカスが置かれたときにすべてのテキストを選択することを開発者が選択できるように、依存プロパティを追加したいです。必要な動作を設定するために使用するスタイルを作成しましたが、ブール依存関係プロパティに基づいて動作を実行するかどうかを開発者に決定してもらいたいです。それがどのように行われるかはわかりません。
解決
このためにXamDateTimeEditorから継承したと仮定します。
「標準」を参照するコードを記述できる場合(clr)プロパティ、次に行ってもいい:
- DependencyPropertyの宣言
-
バッキングフィールドを削除し、バッキングフィールドの代わりにDependencyPropertyにアクセスするように標準プロパティの実装を置き換えます。
public class MyXamDateTimeEditor : XamDateTimeEditor { public static readonly DependencyProperty IsSelectOnFocusEnabledProperty = DependencyProperty.Register("IsSelectOnFocusEnabled", typeof(bool), typeof(MyXamDateTimeEditor), new UIPropertyMetadata(false)); public boolIsSelectOnFocusEnabled { get { return (bool)GetValue(IsSelectOnFocusEnabledProperty); } set { SetValue(IsSelectOnFocusEnabledProperty, value); } } }
その後、コードでIsSelectOnFocusEnabledにアクセスすると、Dependencyプロパティの現在の値が返されます。
プロパティが変更されるたびに通知を受信するように設定することもできますが、なぜそうなるのかわかりません。
このトリックには別のオプションもあり、必要に応じて継承と添付プロパティを使用しません。
更新:
OK、要求されたので、ここで任意のテキストボックスでそれを実現する方法があります。別のタイプのコントロールで実行するために使用するイベントに簡単に変換できるはずです。
public class TextBoxBehaviors
{
public static bool GetIsSelectOnFocusEnabled(DependencyObject obj)
{
return (bool)obj.GetValue(IsSelectOnFocusEnabledProperty);
}
public static void SetIsSelectOnFocusEnabled(DependencyObject obj, bool value)
{
obj.SetValue(IsSelectOnFocusEnabledProperty, value);
}
public static readonly DependencyProperty IsSelectOnFocusEnabledProperty =
DependencyProperty.RegisterAttached("IsSelectOnFocusEnabled", typeof(bool),
typeof(TextBoxBehaviors),
new UIPropertyMetadata(false, new PropertyChangedCallback(OnSelectOnFocusChange)));
private static void OnSelectOnFocusChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is TextBox)
{
var tb = d as TextBox;
if ((bool)e.NewValue)
{
tb.GotFocus += new RoutedEventHandler(tb_GotFocus);
}
else
{
tb.GotFocus -= new RoutedEventHandler(tb_GotFocus);
}
}
}
static void tb_GotFocus(object sender, RoutedEventArgs e)
{
var tb = sender as TextBox;
tb.SelectAll();
}
}
使用方法は次のとおりです。
<Window x:Class="WpfApplication2.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication2"
Title="Window1" Height="300" Width="300">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBox Text="No Select All" x:Name="TextBox1"/>
<CheckBox Content="Auto Select"
Grid.Column="1"
IsChecked="{Binding Path=(local:TextBoxBehaviors.IsSelectOnFocusEnabled), ElementName=TextBox1, Mode=TwoWay}" />
<TextBox Grid.Row="1" Text="djkhfskhfkdssdkj"
local:TextBoxBehaviors.IsSelectOnFocusEnabled="true" />
</Grid>
</Window>
これは、動作をアクティブにするためのプロパティの設定方法と、必要に応じて他の何かにバインドする方法を示します。 この特定の例は完璧ではないことに注意してください(タブで動作する場合、コントロール内をクリックすると、テキストボックスにはテキストを実際に選択解除する内部ロジックがありますが、これは添付プロパティを介してコントロールにビヘイビアをアタッチする方法の例にすぎません) 。