C# WPF更新边框样式块文本触发器

C# WPF更新边框样式块文本触发器,c#,wpf,xaml,data-binding,app.xaml,C#,Wpf,Xaml,Data Binding,App.xaml,使用文本框中的触发器更新父边框是否有效?我尝试使用数据模板方法。我无法使用它,因为我试图在数据上下文中传递两个属性。一个用于占位符,另一个用于绑定User.Email 如果有更好的方法来实现这一成就,我开始使用WPF。 我也尝试过使用这种方法。创建了一个ControlTemplate并使用ScrollViewer x:Name=“PART\u ContentHost”但样式不会更新 关键是a如何以更有效的方式保持绑定和更新父边界 鉴于: <Border Style="{DynamicRes

使用文本框中的触发器更新父边框是否有效?我尝试使用数据模板方法。我无法使用它,因为我试图在数据上下文中传递两个属性。一个用于占位符,另一个用于绑定User.Email

如果有更好的方法来实现这一成就,我开始使用WPF。 我也尝试过使用这种方法。创建了一个ControlTemplate并使用ScrollViewer x:Name=“PART\u ContentHost”但样式不会更新

关键是a如何以更有效的方式保持绑定和更新父边界

鉴于:

<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>


要在控件中显示错误时的某些元素,需要使用附加属性。您可能会发现post很有用,或者查看MahApps.Metro中的。

您在边框触发器中使用
RelativeSource={RelativeSource-AncestorType={x:Type TextBox}
。祖先不是后代<代码>文本框放在
边框内
那么为什么要使用
存储类型
搜索
文本框