-1
WPF 텍스트 상자에 개체 틀을 만들고 있습니다. 내가 stackoverflow에 힌트의 코드가있어. 이 모든 텍스트 상자와 함께 작동합니다 그래서 그것을 사용자 정의하고C# WPF 바인딩 DependencyProperty가 작동하지 않습니다. 텍스트 상자
<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 파일 : DependecyProperty 클래스
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));
}
}
와
<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>
텍스트 상자 그래서 난 XAML 파일에 기록 된 동적 힌트를 원한다. 누군가가 나에게이 점이 잘못되었다고 말할 수 있습니까? 나는 프로젝트에서 다른 힌트를 여러 번 Textbox를 사용하고 싶습니다.
일하고 일하지 않는 것을 분명히하지 못합니다. – kenny
@kenny 질문을 업데이트했습니다. 이제 내가 원하는 것을 얻을 수 있습니다. –
이 스레드가 대답 할 것이라고 생각합니다 : https://stackoverflow.com/questions/13631491/how-can-i-bind-to-an-attached-property-in-a-style-resource –