【问题标题】:WPF Update Border Style BlockText triggerWPF 更新边框样式 BlockText 触发器
【发布时间】:2017-05-26 17:24:39
【问题描述】:

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

如果存在更好的方法来实现这一成就,我开始使用 WPF。 我也尝试过使用这种方法。创建了一个 ControlTemplate 并使用 ScrollViewer x:Name="PART_ContentHost" 但样式没有更新。

重点是如何以更有效的方式保持绑定和更新父边框?

在视图中:

<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

标签: c# wpf xaml data-binding app.xaml


【解决方案1】:

要在控件中显示一些错误元素,您需要使用Validation.ErrorTemplate 附加属性。您可能会发现 this 的帖子很有用,或者查看 MahApps.Metro 中的 error template

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多