locked
Handling Popup with Togglebutton RRS feed

  • Question

  • I have a Toggle button and a popup in Xaml. popup's IsOpen bound to toggle button's IsChecked and the Popup's StaysOpen is set to false. So the popup will get open when the toggle button checked and close if unchecked. This works fine.

    My problem is i have opened the popup and click away from popup, this close my popup due to its stays open false and if i tried to open the popup by clicking the toggle button, popup not get open. Since it is in checked state, and its turn into unchecked state. so its not open the popup.

    Any idea, How may i handle this?


    Sankar

    Friday, May 10, 2013 4:39 AM

Answers

All replies

  • Have you considered a different control altogether?

    Maybe just a grid or any sort of panel which opens on top of most of the UI and you collapse it.


    Friday, May 10, 2013 7:02 AM
  • Hello Sankar.

    Have you tried setting your IsOpen binding of the Popup to TwoWay and use the UpdateSourceTrigger=PropertyChanged?  That would uncheck your toggle button when the popup closes.

    IsOpen="{Binding IsChecked, ElementName=toggleButton, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"

    ~Christine
    Friday, May 10, 2013 2:35 PM
  • Hi Christine,

    Yes, I have used the Update source trigger, but still cant achieve my requirement.


    Sankar

    Friday, May 10, 2013 2:43 PM
  • Not sure why there is a problem.  The following XAML does what you want with no problems.

    XAML:

    <Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
        <Grid>
            <ToggleButton x:Name="toggle" Margin="72,21,324,260">Click Me!</ToggleButton>
            <Popup StaysOpen="True" IsOpen="{Binding ElementName=toggle, Path=IsChecked}">
                <TextBlock Background="Red" Foreground="Wheat" >Is Show</TextBlock>
            </Popup>
        </Grid>
    </Window>
    

    Hope this helps

    LS


    Lloyd Sheen

    Friday, May 10, 2013 4:22 PM
  • After testing a bit, I can see the problems when StaysOpen is false.

    Take a look at this post...

    http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/f0502813-9c4f-4b45-bab8-91f98971e407

    They give a very detailed description on reproducing the problem, what is happening and a solution.

    In addition the last post in that thread presents a different solution if working with commands. 

    Hope you can get it worked out quickly and easily.

    ~Christine

    • Marked as answer by Lisa Zhu Tuesday, June 4, 2013 3:55 PM
    Friday, May 10, 2013 6:16 PM