如何关闭Silverlight中的弹出窗口?

时间:2010-03-05 12:16:56

标签: silverlight

我有ListBox。当我点击ListBox项目时,我必须在弹出窗口中显示项目信息但是在单击一边后它不会关闭。我在itemsselected事件中创建弹出窗口。如何处理弹出窗口关闭?

3 个答案:

答案 0 :(得分:4)

一种方法是创建一个透明背景的画布,在打开弹出窗口并附加到鼠标按下事件以关闭弹出窗口的同时使其可见。像这样: -

的Xaml: -

<Grid x:Name="LayoutRoot" Background="White" >

    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>

        <Popup x:Name="MyPopup" Closed="MyPopup_Closed" HorizontalOffset="100" VerticalOffset="100" Opened="Popup_Opened">
            <ListBox x:Name="PopupChild" MaxHeight="300" LostFocus="PopupChild_LostFocus">
                <sys:String>Hello World</sys:String>
            </ListBox>
        </Popup>

        <Button Content="Open Popup" Grid.Row="1" Click="Button_Click" />

    <Canvas x:Name="PopupOpen" Visibility="Collapsed" Background="Transparent" Grid.RowSpan="2" MouseLeftButtonDown="PopupOpen_MouseLeftButtonDown" />

</Grid>

代码: -

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        MyPopup.IsOpen = true;
    }

    private void Popup_Opened(object sender, EventArgs e)
    {
        PopupOpen.Visibility = Visibility.Visible;
    }

    private void PopupChild_LostFocus(object sender, RoutedEventArgs e)
    {
        MyPopup.IsOpen = false;
    }

    private void PopupOpen_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        MyPopup.IsOpen = false;
    }

    private void MyPopup_Closed(object sender, EventArgs e)
    {
        PopupOpen.Visibility = Visibility.Collapsed;
    }

请注意,如果您的弹出窗口包含一个可以接收您同时处理焦点的控件LostFocus,这一点非常重要。

答案 1 :(得分:1)

这类似于我的问题。看看How to dismiss a popup in Silverlight when clicking outside of the control?。我在我的解决方案中发布了一个扩展方法,它非常有助于在弹出窗口时关闭弹出窗口。

答案 2 :(得分:0)

我不太清楚“点击一边”是什么意思,因为弹出窗口采用模态方式。

您应该将弹出窗口设置为ChildWindow。然后你可以处理Closed事件。

这是一个非常简单的示例,它显示了主窗口中列表框中的选定字符串。

首先是主窗口:

<UserControl x:Class="PopupTest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">

<Grid x:Name="LayoutRoot" Background="White">
    <StackPanel Orientation="Vertical">
        <ListBox x:Name="SomeList" Width="100" Height="100"  />
        <TextBlock x:Name="DialogResult" Width="100" />
    </StackPanel>
</Grid>

在代码隐藏中,当列表选择发生变化时会触发弹出窗口。只需设置一个Closed处理程序。在这个例子中,我只是将所选列表项放入文本块中,然后在关闭弹出窗口时,我只是将对话框结果放在主窗口的文本块中(以显示用户是否按下了确定或取消)。

    public MainPage()
    {
        InitializeComponent();
        SomeList.SelectionChanged += new SelectionChangedEventHandler(SomeList_SelectionChanged);

        SomeList.Items.Add("one");
        SomeList.Items.Add("two");
        SomeList.Items.Add("three");
    }

    void SomeList_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var popup = new SomePopup();
        popup.Closed += new EventHandler(popup_Closed);
        popup.ChosenItem.Text = (string)SomeList.SelectedItem;
        DialogResult.Text = "";
        popup.Show();
    }

    void popup_Closed(object sender, EventArgs e)
    {
        var popup = sender as SomePopup;
        if (popup.DialogResult == true)
            DialogResult.Text = "Ok";
        else
            DialogResult.Text = "Cancel";
    }

当用户按下Ok或Cancel时弹出窗口关闭,因为DialogResult值是在弹出窗口的代码隐藏中设置的:

        private void OKButton_Click(object sender, RoutedEventArgs e)
    {
        this.DialogResult = true;
    }

    private void CancelButton_Click(object sender, RoutedEventArgs e)
    {
        this.DialogResult = false;
    }