IDataErrorInfo覆盖组合框错误样式

时间:2018-12-14 22:32:15

标签: c# wpf xaml mvvm idataerrorinfo

我有一个组合框,用于验证数据错误。当使用组合框检测到错误时,我希望背景变为红色,并且当用户将鼠标悬停在其上方时,将显示一个标签提示。到目前为止,我已经尝试添加

<Trigger Property="Validation.HasError" Value="True">
                            <Setter Property="Background" Value="Red" />
                            <Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0].ErrorContent}" />
                        </Trigger>

这将导致使用默认的轮廓红色,并且标签提示显示错误。

combo box error

如上面的屏幕截图所示,发生错误时,其他文本框的背景为红色,但组合框仍具有默认轮廓。

 <!-- Month combo box styling with attempt at adding error trigger removed-->
    <Style x:Key="MonthComboBoxDropDown" TargetType="{x:Type ComboBox}" >
        <Setter Property="Foreground" Value="{StaticResource WhiteColorBrush}" />
        <Setter Property="FontSize" Value="{StaticResource FontSize20}" />
        <Setter Property="FontFamily" Value="{StaticResource LatoRegular}" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ComboBox">
                    <Grid>
                        <ToggleButton Grid.Column="2" Focusable="false" IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}" >
                            <ToggleButton.Template>
                                <ControlTemplate>
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="5*" />
                                            <ColumnDefinition Width="*" />
                                        </Grid.ColumnDefinitions>

                                        <Border x:Name="Border" Background="{StaticResource DarkGreyBrush}" Grid.ColumnSpan="2" BorderThickness="0" />
                                        <Path x:Name="Arrow" Grid.Column="1"  Fill="{StaticResource WhiteColorBrush}" HorizontalAlignment="Center" VerticalAlignment="Center" Data="M 0 0 L 4 4 L 8 0 Z"/>

                                    </Grid>
                                    <ControlTemplate.Triggers>
                                        <Trigger Property="ToggleButton.IsMouseOver" Value="true">
                                            <Setter TargetName="Border" Property="Background" Value="{StaticResource LightGreyBrush}" />
                                        </Trigger>
                                        <Trigger Property="ToggleButton.IsChecked" Value="true">
                                            <Setter TargetName="Border" Property="Background" Value="{StaticResource LightGreyBrush}" />
                                        </Trigger>
                                    </ControlTemplate.Triggers>
                                </ControlTemplate>
                            </ToggleButton.Template>
                        </ToggleButton>
                        <ContentPresenter Name="ContentSite" IsHitTestVisible="False"  Content="{TemplateBinding SelectionBoxItem}" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" Margin="3" />
                        <TextBox x:Name="PART_EditableTextBox" Visibility="Hidden" MinWidth="120" />
                        <Popup Name="Popup" Placement="Bottom" IsOpen="{TemplateBinding IsDropDownOpen}" AllowsTransparency="True"  Focusable="False" PopupAnimation="Slide">
                            <Grid  Name="DropDown" SnapsToDevicePixels="True" MinWidth="{TemplateBinding ActualWidth}" MaxHeight="{TemplateBinding MaxDropDownHeight}">
                                <Border x:Name="DropDownBorder" Background="{StaticResource DarkGreyBrush}" />
                                <ScrollViewer SnapsToDevicePixels="True" Foreground="{StaticResource WhiteColorBrush}" FontFamily="{StaticResource LatoRegular}" FontSize="{StaticResource FontSize20}" VerticalAlignment="Center">
                                    <StackPanel IsItemsHost="True" />
                                </ScrollViewer>
                            </Grid>
                        </Popup>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

有人可以告诉我我在做什么错吗?

1 个答案:

答案 0 :(得分:0)

Border中的ControlTemplate始终将DarkGreyBrush用作Background元素的Border。请改用TemplateBinding

<Style x:Key="MonthComboBoxDropDown" TargetType="{x:Type ComboBox}" >
    <Setter Property="Background" Value="{StaticResource DarkGreyBrush}" />
    <Setter Property="Foreground" Value="{StaticResource WhiteColorBrush}" />
    <Setter Property="FontSize" Value="{StaticResource FontSize20}" />
    <Setter Property="FontFamily" Value="{StaticResource LatoRegular}" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ComboBox">
                <Grid>
                    <ToggleButton Grid.Column="2" Focusable="false" IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}" >
                        <ToggleButton.Template>
                            <ControlTemplate>
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="5*" />
                                        <ColumnDefinition Width="*" />
                                    </Grid.ColumnDefinitions>

                                    <Border x:Name="Border" Background="{TemplateBinding Background}" Grid.ColumnSpan="2" BorderThickness="0" />
                                    <Path x:Name="Arrow" Grid.Column="1"  Fill="{StaticResource WhiteColorBrush}" HorizontalAlignment="Center" VerticalAlignment="Center" Data="M 0 0 L 4 4 L 8 0 Z"/>

                                </Grid>
                                <ControlTemplate.Triggers>
                                    <Trigger Property="ToggleButton.IsMouseOver" Value="true">
                                        <Setter TargetName="Border" Property="Background" Value="{StaticResource LightGreyBrush}" />
                                    </Trigger>
                                    <Trigger Property="ToggleButton.IsChecked" Value="true">
                                        <Setter TargetName="Border" Property="Background" Value="{StaticResource LightGreyBrush}" />
                                    </Trigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </ToggleButton.Template>
                    </ToggleButton>
                    <ContentPresenter Name="ContentSite" IsHitTestVisible="False"  Content="{TemplateBinding SelectionBoxItem}" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" Margin="3" />
                    <TextBox x:Name="PART_EditableTextBox" Visibility="Hidden" MinWidth="120" />
                    <Popup Name="Popup" Placement="Bottom" IsOpen="{TemplateBinding IsDropDownOpen}" AllowsTransparency="True"  Focusable="False" PopupAnimation="Slide">
                        <Grid  Name="DropDown" SnapsToDevicePixels="True" MinWidth="{TemplateBinding ActualWidth}" MaxHeight="{TemplateBinding MaxDropDownHeight}">
                            <Border x:Name="DropDownBorder" Background="{StaticResource DarkGreyBrush}" />
                            <ScrollViewer SnapsToDevicePixels="True" Foreground="{StaticResource WhiteColorBrush}" FontFamily="{StaticResource LatoRegular}" FontSize="{StaticResource FontSize20}" VerticalAlignment="Center">
                                <StackPanel IsItemsHost="True" />
                            </ScrollViewer>
                        </Grid>
                    </Popup>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

在发布DarkGreyBrush时,应包括所有引用的资源,例如WhiteColorBrushStyle