-1
是否有可能使用触发器从TextBox更新父边框是否有效?我尝试了使用数据模板方法。我无法使用它,因为我试图在数据上下文中传递两个属性。一个用于占位符,另一个用于绑定User.Email。WPF更新边框样式BlockText触发器
如果存在一个更好的方法来实现这一成就,我开始使用WPF。 我也尝试过使用这种方法。创建一个ControlTemplate并使用ScrollViewer x:Name="PART_ContentHost"
,但样式不更新。
重点是如何可以保持绑定和更有效的方式更新父边界?
在View:
<Border Style="{DynamicResource TextBoxBorderStyle}" >
<Grid>
<TextBlock Text="{Binding Path=UserPlaceholder}"
Style="{StaticResource PlaceHolderTextStyle}">
<TextBlock.Visibility>
<MultiBinding Converter="{StaticResource textInputToVisibilityConverter}">
<Binding ElementName="Email" Path="Text.IsEmpty" />
<Binding ElementName="Email" Path="IsFocused" />
</MultiBinding>
</TextBlock.Visibility>
</TextBlock>
<TextBox Name="Email"
Background="Transparent"
Style="{StaticResource textBoxBase}"
Text="{Binding Path=UserCredentials.Email,
Mode=TwoWay,
TargetNullValue='',
ValidatesOnDataErrors=True,
UpdateSourceTrigger=PropertyChanged}">
</TextBox>
</Grid>
</Border>
在应用边框样式:
<Style x:Key="TextBoxBorderStyle"
TargetType="{x:Type Border}">
<Setter Property="BorderBrush"
Value="{DynamicResource TextBoxBorderBrush}"/>
<Setter Property="Background"
Value="{StaticResource TextBoxBackgroundBrush}"/>
<Setter Property="HorizontalAlignment"
Value="Stretch"/>
<Setter Property="VerticalAlignment"
Value="Center"/>
<Setter Property="BorderThickness"
Value="2"/>
<Setter Property="CornerRadius"
Value="{StaticResource DefaultBorder}"/>
<Setter Property="Height"
Value="50"/>
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type TextBox}}, Path=Validation.HasError}" Value="True">
<Setter Property="BorderBrush"
Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
在应用文本块风格:
<Style x:Key="textBoxBase" TargetType="{x:Type TextBox}">
<Setter Property="HorizontalAlignment"
Value="Stretch"/>
<Setter Property="VerticalAlignment"
Value="Center"/>
<Setter Property="Foreground"
Value="{StaticResource TextBoxForegroundBrush}"/>
<Setter Property="FontSize"
Value="16"/>
<Setter Property="Margin"
Value="{StaticResource DefaultMargin}"/>
<Setter Property="FontFamily"
Value="{StaticResource DefaultFontFamily}"/>
<Setter Property="BorderThickness"
Value="0"/>
</Style>
在边界触发器中使用'RelativeSource = {RelativeSource AncestorType = {x:Type TextBox}}''。祖先不是后代。 'TextBox'被放在'Border'里面,所以你为什么要用'AncestorType'搜索'TextBox'? – Maxim