I want a TextBox with the text WaterMark. I am using this solution that works great as intended.
Since I have a couple of TextBoxes in the control, I want to make it a bit dynamic. So I use (the first time) an attached property, but I cannot get it to work. There are no compilation errors, but the tag statement cannot be allowed in XAML ... Content={Binding Path=view:SomeClass.Tag, RelativeSource=...
What is wrong here?
I did it in XAML
<StackPanel Grid.Row="1" TextBlock.FontSize="12"> <TextBox Style="{DynamicResource TextBoxWaterMark}" view:SomeClass.Tag="Search" /> </StackPanel>
Style
<RibbonWindow.Resources> <Style xmlns:sys="clr-namespace:System;assembly=mscorlib" x:Key="TextBoxWaterMark" TargetType="{x:Type TextBox}"> <Style.Resources> <VisualBrush x:Key="CueBannerBrush" AlignmentX="Left" AlignmentY="Center" Stretch="None"> <VisualBrush.Visual> <Label Content="{Binding Path=view:SomeClass.Tag, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type view:SomeClass}}}" 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="IsMouseCaptured" Value="True"> <Setter Property="Background" Value="White" /> </Trigger> </Style.Triggers> </Style> </RibbonWindow.Resources>
DependencyObject
public static class SomeClass { public static readonly DependencyProperty TagProperty = DependencyProperty.RegisterAttached( "Tag", typeof(object), typeof(SomeClass), new FrameworkPropertyMetadata(null)); public static object GetTag(DependencyObject dependencyObject) { return dependencyObject.GetValue(TagProperty); } public static void SetTag(DependencyObject dependencyObject, object value) { dependencyObject.SetValue(TagProperty, value); } }
c # wpf textbox
Rolfi Sep 17 '13 at 7:24 2013-09-17 07:24
source share