1
votes

I've looked around, but can't specifically find my issue. I know that the default "Error" handling within WPF puts an "Adorner" around controls in case there are any errors based on IDataErrorInfo or Validataion rules failing a given control. That's all good and fine, however, with a tabbed page interface, if any controls are so flagged as invalid, they are properly adorned in red border. However, as soon as you go from tab page 1 to 2 and back to 1, all the adorners are gone (bad). This was already asked, and solution accepted, but was looking for a better alternative.

So, I went to my "Themes" declaration, and for the textbox control, I just said to set the entire background color of the control to red and not just the border. Without any fancy forced triggering via Notify on Property Changed, if I swap between pages, the red background of the entire textbox remains constant.

Now, on to the combobox control. For those who have customized their own, or even looked into the default MS version of the control, its actually a clustered mess of controls, grids, columns, buttons, etc to make the magic of combobox work. In brief...

ControlTemplate Grid (two columns, one for text display of chosen, second column for the drop-down arrow) Border spanning both columns Path ( line drawing / glyph for the drop-down image for combobox )

ControlTemplate TargetType Textbox (as part of the entire combobox set) Border specifically "PART_ContentHost"

ControlTemplate of combobox Grid Toggle Button Dropdown exposed showing list other triggers..

Finally, the main ComboBox declaration which is templated by above components.

Anyhow, I can't for the life of me get this. In the "Toggle Button" area of the combobox declaration, I have a trigger to change the background to an OBVIOUS off color for proof of testing the trigger working and in the right location within the ControlTemplate declarations.

So, knowing this is the correct place within the combobox declarations, I want to supersede the green background color with red if there's an error with the data. I KNOW the overall "Validation.HasError" is properly getting triggered as the native error handler shows. No matter how / where within the template I try to change the background color to red, it does NOT work. I've even tried doing DataTriggers, using converters, trying multiple properties, but it appears not to be cooperating.

Any suggestions? This is getting really annoying.

1

1 Answers

2
votes

FINALLY, got it... and not as obvious as I would have guessed. Anyhow, here's what I've found. If you went with a sample from Microsoft's template of the combobox, they first provide the overall two-column "ToggleButton" declaration

<ControlTemplate TargetType="ToggleButton" 
   x:Key="baseComboBoxToggleButton" >

  ... blah blah...

</ControlTemplate>

Then, the declaration for the "Display Value" of the combobox

<ControlTemplate TargetType="TextBox" x:Key="ComboBoxTextBox" >
   <Border x:Name="PART_ContentHost" Focusable="False" 
           Background="{TemplateBinding Background}" />
</ControlTemplate>

Then, tie them together as one Combobox "wrapper" declaration

<ControlTemplate TargetType="ComboBox" x:Key="ComboBoxGridControlTemplate" >
   <Grid x:Name="GridComboWrapper">
      <!-- This is the dropdown button that POINTS TO THE "baseComboBoxToggleButton at the top -->
      <ToggleButton Name="ToggleButton" 
        Template="{StaticResource baseComboBoxToggleButton}" 
        Grid.Column="2" Focusable="false"
        IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay,
                RelativeSource={RelativeSource TemplatedParent}}"
                ClickMode="Press" >
      </ToggleButton>

      ...
      rest of the content presenter,
      EDIT(able) textbox area,
      popup area of combobox when in drop-down mode

   </Grid> 


   <ControlTemplate.Triggers>

      <!-- PUT THE VALIDATION CHECK HERE -->
      <Trigger Property="Validation.HasError" Value="true">
         <Setter Property="ToolTip" 
                 Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                 Path=(Validation.Errors)[0].ErrorContent}"/>


         <!-- THIS IS THE CRITICAL COMPONENT... I HAD TO EXPLICITLY TELL
              The TagetName as the "ToggleButton" and change ITs Background property
              and it now works -->
         <Setter TargetName="ToggleButton" Property="Background" 
                 Value="{StaticResource BrushDataInvalidBorder}" />
      </Trigger>

   </ControlTemplate.Triggers>
</ControlTemplate>

So, now it works as intended and doesn't loose any adorner just because the active page on a given form changes and clears it... its static to each individual control as expected... Wow... what a PITA this one was.

Hope it helps someone ELSE in the future from excessive head banging against a wall while learning this nested level of stuff.