【问题标题】:Cannot close silverlight popup with custom control in it无法关闭带有自定义控件的 Silverlight 弹出窗口
【发布时间】:2015-11-16 16:52:56
【问题描述】:

我有一个带有两个文本框的 silverlight 应用程序。当我专注于其中一个时,我希望显示带有“虚拟”键盘的弹出窗口(有效)。但是,问题是当我想关闭弹出窗口时。我希望能够用我的“X”按钮(屏幕上的红色)关闭它。我该怎么做?我尝试了一切——代表,INotifyProperyChanged。似乎没有任何效果。在我的 XAML 代码中,您可以看到我无法访问 MainPage 中的任何按钮 - 只能从自定义控件的代码中访问。这就是问题所在。

<Grid x:Name="LayoutRoot" Background="#DB1E1E1E" Margin="0,0,-89,-114">

    <TextBox x:Name="txt1"
        HorizontalAlignment="Left" Height="23"
        Margin="10,10,0,0" TextWrapping="Wrap"
        Text="TextBox" VerticalAlignment="Top"
        Width="398" GotFocus="txt1_GotFocus"/>
    <TextBox
        HorizontalAlignment="Left"
        Height="23"
        Margin="10,82,0,0"
        TextWrapping="Wrap"
        Text="TextBox"
        VerticalAlignment="Top"
        Width="398"/>

    <Popup x:Name="popup" IsOpen="True" AllowDrop="True">
        <Grid x:Name="theBack" Background="Black" Margin="80,196,114,24">
            <Keyboard:KeyboardControl x:Name="keyboard" Margin="0,-10,0,10"/>
        </Grid>
    </Popup>

    <Button x:Name="btn"
        Content="Button"
        HorizontalAlignment="Left"
        Margin="508,114,0,0"
        VerticalAlignment="Top"
        Width="75"
        Click="btn_Click"/>

</Grid>

【问题讨论】:

    标签: c# wpf class silverlight popup


    【解决方案1】:

    无绑定:

    在 KeyboardControl.xaml.cs 中为您的控件定义一个自定义处理程序和一个单击关闭按钮的方法:

    public event RoutedEventHandler CloseClick;
    
    private void ButtonX_Click(object sender, RoutedEventArgs e)
    {
        if (CloseClick != null)
            CloseClick(sender, e);
    }
    

    然后在 KeyboardControl.xaml 中用之前创建的方法分配“X”按钮的“Click”事件

    <Button Content="X" Click="ButtonX_Click"/>
    

    然后在您的 MainPage 中为此更改 KeyboardControl 的布局:

    终于在MainPage.xaml.cs中:

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

    【讨论】:

      猜你喜欢
      • 2016-12-24
      • 1970-01-01
      • 1970-01-01
      • 2021-08-29
      • 2014-01-19
      • 2018-04-03
      • 2017-06-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多