How do I get rid of the red rectangle when my wpf binding validation has failed and the containing panel is no longer visible?

前端 未结 2 724
情深已故
情深已故 2020-12-08 15:20

I have a situation where I am using wpf data binding and validation using the ExceptionValidationRule.

Another part of the solution invovles collapsing some panels a

相关标签:
2条回答
  • 2020-12-08 16:06

    If I remember correctly, this is a known issue. We re-templated textbox to include the following:

    <Setter Property="Validation.ErrorTemplate">
        <Setter.Value>
            <ControlTemplate>
                <ControlTemplate.Resources>
                    <BooleanToVisibilityConverter x:Key="converter" />
            </ControlTemplate.Resources>
                <DockPanel LastChildFill="True">
                    <Border 
                        BorderThickness="1"
                        BorderBrush="Red"
                        Visibility="{Binding ElementName=placeholder, Mode=OneWay, Path=AdornedElement.IsVisible, Converter={StaticResource converter}}">
                        <AdornedElementPlaceholder x:Name="placeholder" />
                    </Border>
                 </DockPanel>
             </ControlTemplate>
        </Setter.Value>
    </Setter>
    
    0 讨论(0)
  • 2020-12-08 16:19

    I have an answer to the problem myself which is to change my button click event which changes the visibility of the panels. This would change to something like this:

    private void Button_Click(object sender, RoutedEventArgs e) {
        if (panel1.Visibility == Visibility.Collapsed) {
            panel1.Visibility = Visibility.Visible;
            DataBoundTextBox.GetBindingExpression(TextBox.TextProperty).UpdateSource();
            panel2.Visibility = Visibility.Collapsed;
        }
        else {
            panel1.Visibility = Visibility.Collapsed;
            DataBoundTextBox.GetBindingExpression(TextBox.TextProperty).UpdateTarget();
            panel2.Visibility = Visibility.Visible;
        }
    }
    

    The UpdateSource() and UpdateTarget() have the effect of reapplying and removing the red rectangle, but this seems like an ugly hack. Surely the wpf framework should be hiding the red rectangle for me when the containing panel is collapsed. Any cleaner fix that doesn't require me to fiddle with the binding expression gets my vote.

    Thanks,

    Sam

    0 讨论(0)
提交回复
热议问题