切换按钮控制弹出窗口

时间:2018-03-02 04:40:39

标签: c# wpf

this is how the popup looks at the beginning and when i press "profiles" for the first time , the popup reopens rather than going down. but the second time press the profile button , the popup works as desired,the issue is there only for the first time

当我们点击切换按钮时,会打开一个弹出窗口。我编写了一个转换器,只要我们再次单击切换按钮或者单击弹出窗口以外的任何位置,弹出窗口就会关闭。 XAML代码就是这个

<ToggleButton x:Name="m_XYProfileBtn"  Content="Profiles" Height="24" Style="{DynamicResource StatusFlatToggle}"
                               Visibility="{Binding IsToggleStatusVisibility}" IsChecked="{Binding IsPopUpOpen}"  />
<Popup Margin="5" Name="ProfilePopup" HorizontalAlignment="Center" VerticalAlignment="Center" 
                           Width="400" Height="500" AllowsTransparency="True"
                           IsOpen="{Binding IsPopUpOpen}"
                           Focusable="False"
                           PopupAnimation="Slide"
                           Placement="Top"
                           VerticalOffset="-4"
                           HorizontalOffset="2"
                           >
<Popup.StaysOpen>
    <MultiBinding Converter="{StaticResource MultiBinding_StayOpen}">
        <Binding ElementName="m_XYProfileBtn"  Path="IsMouseOver"/>
        <Binding ElementName="Pin" Path="IsChecked" />
    </MultiBinding>
</Popup.StaysOpen>
<Border BorderThickness="2" BorderBrush="Gray" Background="White" >
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="0.05*" />
            <RowDefinition Height="0.95*" />
        </Grid.RowDefinitions>
        <CheckBox Name="Pin" Content="Pin" Style ="{StaticResource PinBox}" HorizontalAlignment="Right"/>
        <Grid Name="xProfileGrid" Grid.Row="1" />
    </Grid>
</Border>

和C#转换器代码是这个

public class MultiBinding_StayOpen : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {

        bool toggleIsMouseOver;
        bool pinIsChecked;

        //System.Threading.Thread.Sleep(3000);
        toggleIsMouseOver = System.Convert.ToBoolean(values[0]);
        pinIsChecked = System.Convert.ToBoolean(values[1]);

        if (pinIsChecked == true)
            return true;
        else if (toggleIsMouseOver == true)
            return true;
        else if (toggleIsMouseOver == false)
        {
            System.Threading.Thread.Sleep(300);
            return false;
        }

        return true;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

这样可以正常工作,但是当您第一次单击切换按钮时,弹出窗口会再次打开而不是关闭。之后它完美地运作。有什么建议我可以解决这个问题吗?

0 个答案:

没有答案