2017-11-04 17 views
-1

WPFテキストボックスにプレースホルダを作成しています。私はヒントのコードをstackoverflowで得た。それは、すべてのテキストボックスで動作しますので、私はそれをカスタマイズしていますC#WPFバインディングDependencyProperty Not Workingテキストボックス

<Window x:Class="App.Test" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:local="clr-namespace:App" 
      mc:Ignorable="d" 
      Title="Test" Height="300" Width="300"> 
     <Window.Resources> 
      <Style x:Key="TextBoxWithHintStyle" TargetType="TextBox" xmlns:sys="clr-namespace:System;assembly=mscorlib"> 
       <Style.Resources> 
        <VisualBrush x:Key="CueBannerBrush" AlignmentX="Left" AlignmentY="Center" Stretch="None"> 
         <VisualBrush.Visual> 
          <Label Content="search" Foreground="LightGray" /> 
         </VisualBrush.Visual> 
        </VisualBrush> 
       </Style.Resources> 
       <Style.Triggers> 
        <Trigger Property="Text" Value="{x:Static sys:String.Empty}"> 
         <Setter Property="Background" Value="{StaticResource CueBannerBrush}" /> 
        </Trigger> 
        <Trigger Property="Text" Value="{x:Null}"> 
         <Setter Property="Background" Value="{StaticResource CueBannerBrush}" /> 
        </Trigger> 
        <Trigger Property="IsKeyboardFocused" Value="True"> 
         <Setter Property="Background" Value="White" /> 
        </Trigger> 
       </Style.Triggers> 
      </Style> 

     </Window.Resources> 

     <Grid> 
      <TextBox Style="{StaticResource TextBoxWithHintStyle}" Height="auto" Padding="5px" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="auto" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Stretch"/> 
     </Grid> 
    </Window> 

静的なヒントと

作業するコード。

XAMLファイル:

<Window x:Class="App.Test" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:App" 
     mc:Ignorable="d" 
     Title="Test" Height="300" Width="300"> 
    <Window.Resources> 
     <Style x:Key="TextBoxWithHintStyle" TargetType="TextBox" xmlns:sys="clr-namespace:System;assembly=mscorlib"> 
      <Style.Resources> 
       <VisualBrush x:Key="CueBannerBrush" AlignmentX="Left" AlignmentY="Center" Stretch="None"> 
        <VisualBrush.Visual> 
         <Label Content="{Binding Path=(local:TextBoxWithHint.Watermark),RelativeSource={RelativeSource Self}}" Foreground="LightGray" /> 
        </VisualBrush.Visual> 
       </VisualBrush> 
      </Style.Resources> 
      <Style.Triggers> 
       <Trigger Property="Text" Value="{x:Static sys:String.Empty}"> 
        <Setter Property="Background" Value="{StaticResource CueBannerBrush}" /> 
       </Trigger> 
       <Trigger Property="Text" Value="{x:Null}"> 
        <Setter Property="Background" Value="{StaticResource CueBannerBrush}" /> 
       </Trigger> 
       <Trigger Property="IsKeyboardFocused" Value="True"> 
        <Setter Property="Background" Value="White" /> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 

    </Window.Resources> 

    <Grid> 
     <TextBox Style="{StaticResource TextBoxWithHintStyle}" local:TextBoxWithHint.Watermark="Search" Height="auto" Padding="5px" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="auto" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Stretch"/> 
    </Grid> 
</Window> 

DependecyPropertyクラスでのTextBox

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

namespace App 
{ 
    public class TextBoxWithHint : TextBox 
    { 
     public static string GetWatermark(DependencyObject obj) 
     { 
      return (string)obj.GetValue(WatermarkProperty); 
     } 

     public static void SetWatermark(DependencyObject obj, string value) 
     { 
      obj.SetValue(WatermarkProperty, value); 
     } 

     public static readonly DependencyProperty WatermarkProperty = 
      DependencyProperty.RegisterAttached("Watermark", typeof(string), typeof(TextBoxWithHint), new UIPropertyMetadata(string.Empty)); 
    } 
} 

だから私は、XAMLファイルに書かれている動的なヒントが欲しいです。誰かが私にこのことが間違っていると教えてもらえますか?私はプロジェクトで異なるヒントを使用してTextboxを複数回使用したい。

+0

あなたが働いていて働いていないものはわかりません。 – kenny

+0

@kenny質問を更新しました。今あなたは私が望むものを手に入れます。 –

+0

このスレッドはそれに答えるかもしれないと思う:https://stackoverflow.com/questions/13631491/how-can-i-bind-to-an-attached-property-in-a-style-resource –

答えて

0

私は1つのカスタムコントロールとしてコードビハインドでこれを行うように誘惑されるだろう:

public class TextBoxWithHint : TextBox 
{ 
    private readonly VisualBrush vb; 

    public static string GetWatermark(DependencyObject obj) 
    { 
     return (string)obj.GetValue(WatermarkProperty); 
    } 

    public static void SetWatermark(DependencyObject obj, string value) 
    { 
     obj.SetValue(WatermarkProperty, value); 
    } 

    public static readonly DependencyProperty WatermarkProperty = 
     DependencyProperty.RegisterAttached("Watermark", typeof(string), typeof(TextBoxWithHint), new UIPropertyMetadata(string.Empty)); 

    public TextBoxWithHint() 
    { 
     Label l = new Label { Foreground = new SolidColorBrush(Colors.LightGray) }; 

     Binding b = new Binding 
     { 
      Source = this, 
      Path = new PropertyPath("Watermark"), 
      UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged 
     }; 

     BindingOperations.SetBinding(l, ContentControl.ContentProperty, b); 

     vb = new VisualBrush(l) { AlignmentX = AlignmentX.Left, AlignmentY = AlignmentY.Center, Stretch = Stretch.None }; 

     GotKeyboardFocus += TextBoxWithHint_GotKeyboardFocus; 
     LostKeyboardFocus += TextBoxWithHint_LostKeyboardFocus; 

     Initialized += TextBoxWithHint_Initialized; 
    } 

    private void TextBoxWithHint_Initialized(object sender, EventArgs e) 
    { 
     if (string.IsNullOrEmpty(Text) && !IsKeyboardFocused) SetBackgroundWatermark(); 
     else SetBackgroundPlain(); 
    } 

    private void TextBoxWithHint_LostKeyboardFocus(object sender, System.Windows.Input.KeyboardFocusChangedEventArgs e) 
    { 
     if (string.IsNullOrEmpty(Text) && Background.GetType() == typeof(SolidColorBrush)) SetBackgroundWatermark(); 
    } 

    private void TextBoxWithHint_GotKeyboardFocus(object sender, System.Windows.Input.KeyboardFocusChangedEventArgs e) 
    { 
     if (Background.GetType() == typeof(VisualBrush)) SetBackgroundPlain(); 
    } 

    private void SetBackgroundWatermark() { Background = vb; } 
    private void SetBackgroundPlain() { Background = new SolidColorBrush(Colors.White); } 
} 

あなたはその後、(スタイルを必要とせずに)あなたのXAMLでこのコントロールを参照します:

<local:TextBoxWithHint Watermark="Search" ...