【问题标题】:Xaml Border issueXaml 边界问题
【发布时间】:2013-05-28 20:48:20
【问题描述】:

我正在使用HeaderedContentControl 来显示我的标签和文本框,如下所示..

<Style x:Key="ContentBorderStyle" TargetType="Border">
        <Setter Property="BorderBrush" Value="Blue"/>
</Style>

<Style TargetType="HeaderedContentControl" x:Key="BaseLabeledItemStyle">
        <Style.Setters>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="HeaderedContentControl">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition />
                                <ColumnDefinition />
                            </Grid.ColumnDefinitions>
                            <Border Grid.Column="0">
                                <ContentPresenter Name="header"        
                                                            Content="{TemplateBinding Header}"/>
                            </Border>
                            <Border Grid.Column="1"
                                    Style="{StaticResource ContentBorderStyle}">
                                <AdornerDecorator>
                                    <ContentPresenter Name="content"                                         
                                                  Content="{Binding RelativeSource={RelativeSource TemplatedParent},Path=Content}"/>
                                </AdornerDecorator>
                            </Border>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style.Setters>
  </Style>

<HeaderedContentControl
                Style="{StaticResource ResourceKey=BaseLabeledItemStyle}" 
                Header="Emp Name">
                <!--<ContentControl>-->
                <TextBox x:Name="txtName" 
                         Text="{Binding Path=EmpName, Mode=TwoWay, ValidatesOnDataErrors=True}"/>
            </HeaderedContentControl>

当我使用边框画笔作为内容控件的“蓝色”时,我的内容控件始终显示为蓝色边框。当出现验证错误时,我在 Headered 内容控件的边框内使用的文本框将变为红色,因为我正在使用 ValidationOnDataerror。现在我的要求是在验证发生时(即当内部文本框为红色时)甚至将内容边框也更改为红色...

我正在附加一个图像,我的控件是如何填充的。其中 (1) 是我的内容控件的边框,(2) 是我的文本框控件。

如何根据文本框颜色更改内容控件的颜色...

提前谢谢...

【问题讨论】:

    标签: wpf xaml wpf-controls


    【解决方案1】:

    您可以使用DataTrigger 进行额外突出显示

    <Style x:Key="ContentBorderStyle" TargetType="Border">
        <Setter Property="BorderBrush" Value="Blue"/>
        <Style.Triggers>
            <DataTrigger Binding="{Binding (Validation.HasError), ElementName=txtName}" Value="True">
                <Setter Property="BorderBrush" Value="Red"/>                    
            </DataTrigger>
        </Style.Triggers>
    </Style>
    

    仅供参考: 如果您想在除TextBox 之外的任何其他控件上显示Validation.ErrorTemplate,请使用Validation.ValidationAdornerSiteFor Attached Property。例如

    <HeaderedContentControl Validation.ValidationAdornerSiteFor="{Binding ElementName=txtName}"
    

    <Style x:Key="ContentBorderStyle" TargetType="Border">
        <Setter Property="BorderBrush" Value="Blue"/>
        <Setter Property="Validation.ValidationAdornerSiteFor" Value="{Binding ElementName=txtName}"/>
    </Style>
    

    【讨论】:

    • 感谢您的回复。由于我将这种样式用于多个控件,因此我不应该使用“txtName”。请您帮我如何用我各自的文本框替换“txtName”?
    • 在这里我不仅使用文本框,还使用不同的控件,如日期时间控件和组合框...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-19
    • 1970-01-01
    • 1970-01-01
    • 2019-06-23
    相关资源
    最近更新 更多