【问题标题】:IDataErrorInfo overrode combobox error stylingIDataErrorInfo 覆盖组合框错误样式
【发布时间】:2018-12-14 22:32:14
【问题描述】:

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

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

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

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

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

谁能告诉我我做错了什么?

【问题讨论】:

    标签: c# wpf xaml mvvm idataerrorinfo


    【解决方案1】:

    ControlTemplate 中的Border 始终使用DarkGreyBrush 作为BackgroundBorder 元素。请改用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>
    

    顺便说一句,在发布Style 时,您应该包括所有引用的资源,例如DarkGreyBrushWhiteColorBrush

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-16
      相关资源
      最近更新 更多