MahApps.Metro icon indicating copy to clipboard operation
MahApps.Metro copied to clipboard

TextBox with textboxhelper cleartextbtn doesnt work after visibilty change

Open gil-mak opened this issue 12 years ago • 8 comments

Hi, I have a grid with 2 columns. col 0 is visible and have a button that change the visibility col 1 visible. in col 0 the textbox which works fine:

                <TextBox Grid.Col="0" VerticalAlignment="Center"
                         Text="{Binding Model.ComputerName, Delay=2500,
                                        UpdateSourceTrigger=PropertyChanged,
                                        Mode=TwoWay}"
                         controls:TextboxHelper.ClearTextButton="True"
                         controls:TextboxHelper.Watermark="{Binding Path=Model.TotalCount,
                                                                    StringFormat='{} Any ({0})'}" />

and in col 1 i have the same text box (with different text binding property). the problame is that in col 1 (after visibilty of the grid change to visible) the cleartextbutton doesnt work (it appears but not clearing the text). if i remove the visibility binding from the grid so both columns will be visible it works. any ideas for a solution? thanks.

gil-mak avatar Apr 06 '14 08:04 gil-mak

Hi @gilimak . Can we have a screenshot of this behavior?

AzureKitsune avatar Apr 06 '14 14:04 AzureKitsune

screen

gil-mak avatar Apr 08 '14 06:04 gil-mak

Hi, i have the same problem.

My xmal:

[...] 
<CheckBox HorizontalAlignment="Center" x:Name="ShowPlainPwCb" IsChecked="{Binding IsShowPlainPassword}">Klartext anzeigen</CheckBox>
[...] 
<Grid>
    <ProgressBar x:Name="ClearInputProgressBar" [...] />
    <PasswordBox [...]
                controls:TextboxHelper.ClearTextButton="True"                
                Visibility="{Binding IsChecked,ElementName=ShowPlainPwCb,Converter={StaticResource BooleanToVisibilityConverterReverse}}"/>
    <TextBox [...]
            controls:TextboxHelper.ClearTextButton="True"
            Visibility="{Binding IsChecked,ElementName=ShowPlainPwCb,Converter={StaticResource BooleanToVisibilityConverter}}" />
</Grid>

On both the ClearTextButton is shown and clickable but only the ClearTextButton on first control Visibility="Visible" clears the text. Other do nothing on click. => If i make TextBox first "Visible" its ClearTextButton working and PasswordBox(Hidden) ClearTextButton not => If i make PasswordBox first "Visible" its ClearTextButton working and TextBox(Hidden) ClearTextButton not

Have also no solution for this problem. Thanks for help!

arnoldpa avatar Aug 14 '14 20:08 arnoldpa

Hi Arnold, I fixed it by override the control and added in the ButtonChange command. in the last line i add textbox.UpdateLayout()

gil-mak avatar Aug 19 '14 14:08 gil-mak

Hi gilimak,

thanks for your reply. But i don't understand how you fixed it. Which control do you override?

I found an other solution that fix the problem for me:

  1. Both controls are visible on startup
  2. Add 'Loaded'-event to code behind which set the styles. The styles handle the visibility of the controls.
private void MainView_OnLoaded(object sender, RoutedEventArgs e)
{
    PwPasswordBox.Style = Resources[ResourceKeyConstants.PasswordBoxStyle] as Style;
    PwTextBox.Style = Resources[ResourceKeyConstants.PasswordTextBoxStyle] as Style;
}

arnoldpa avatar Aug 20 '14 16:08 arnoldpa

I've recently encountered this exact issue. I took a look at the TextBoxHelper class and found the culprit behind the failing ClearTextButton.

There are loading events for the PasswordBox, ComboBox and TextBox in TextBoxHelper that attempt to subscribe the ClearTextButton to a clearing event if the button exists. The loading events try to find the button by name in the template ("PART_ClearText") but under some circumstances involving visibility the lookup is done before the template is applied.

Altering TextBoxHelper to call ApplyTemplate() on the TextBox before searching for the ClearTextButton fixes this. As a workaround, you can call ApplyTemplate() in the Loaded event on the window/control which is what both of your solutions achieve in different ways.

Thought I'd share as this issue was the only mention of this problem I could find.

fuzzylee avatar Sep 13 '14 06:09 fuzzylee

I ran into this same issue. I tried calling ApplyTemplate from the Loaded event but it broke the layout of the button within the textbox. I ended up leaving the textbox's container visible until the loaded event fired which got everything working.

JoeRobich avatar Feb 12 '15 18:02 JoeRobich

I can confirm this bug. When the textbox is hidden while loaded, the code

var button = template.FindName("PART_ClearText", textbox) as Button;

in TextBoxHelper.TextBoxLoaded returns null.

Unfortunately, I don't know how to fix it. :disappointed:

thoemmi avatar Feb 12 '15 21:02 thoemmi