2009-05-02 5 views
6

Infragistics XamDateTimeEditorコントロールを使用しています。コントロールにフォーカスが当たったときにすべてのテキストを選択できるように、依存プロパティを追加します。私は、私が望む振る舞いを設定するために使用されるスタイルを作成しましたが、振る舞いがブール型の依存関係プロパティに基づいて実行されるべきかどうかを開発者に決定させたいと思います。私はそれがどのように行われているのか分かりません。コントロールに依存プロパティを追加します。

答えて

16

あなたはこれをXamDateTimeEditorから継承したと仮定します。

あなたは「標準」(CLR)プロパティを参照するコードを書くことができるなら、あなたが行ってもいいです:

  1. はあなたのDependencyProperty
  2. があなたのバッキングフィールドを削除しての実装を置き換える宣言バッキングフィールドの代わりに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にアクセスするときに、それは、依存関係プロパティの現在の値を返します。

また、プロパティが変更されたときに通知を受け取るように設定することもできますが、なぜあなたの場合に表示されるのかわかりません。

また、このトリックには別のオプションがあります。このトリックには、必要に応じて継承および添付プロパティを使用しません。

UPDATE:

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> 

これは動作を有効にするプロパティを設定する方法、および必要な場合は何か他のものにそれをバインドする方法を示しますさあ。 この特定の例は完璧ではないことに注意してください(実際にタブをクリックしてコントロールをクリックすると、テキストボックスには内部ロジックがあり、実際にはテキストの選択が解除されますが、 )。

+0

XamDateTimeEditorがDependencyObjectを拡張しない場合はどうなりますか?コードはコンパイルされません。たとえば、私はBehaviorを拡張しており、それに依存できないプロパティを追加したいと考えています。 – rolls

関連する問題