仅当使用XAML标记时,如何在单击另一个控件时打开WPF弹出窗口? [英] How to open a WPF Popup when another control is clicked, using XAML markup only?

查看:132
本文介绍了仅当使用XAML标记时,如何在单击另一个控件时打开WPF弹出窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个控件,一个TextBlock和一个PopUp.当用户单击文本块上的(MouseDown)时,我要显示弹出窗口.我认为我可以通过Popup上的EventTrigger来做到这一点,但是我不能在EventTrigger中使用设置器,只能启动情节提要.我想在XAML中严格执行此操作,因为这两个控件都在模板中,而且我不知道如何在代码中找到弹出窗口.

I've got two controls, a TextBlock and a PopUp. When the user clicks (MouseDown) on the textblock, I want to display the popup. I would think that I could do this with an EventTrigger on the Popup, but I can't use setters in an EventTrigger, I can only start storyboards. I want to do this strictly in XAML, because the two controls are in a template and I don't know how I'd find the popup in code.

从概念上讲,这是我想要做的,但是不能这样做,因为您不能在EventTrigger中放置setter(就像使用DataTrigger一样):

This is what conceptually I want to do, but can't because you can't put a setter in an EventTrigger (like you can with a DataTrigger):

<TextBlock x:Name="CCD">Some text</TextBlock>

<Popup>
    <Popup.Style>
        <Style>
            <Style.Triggers>
                <EventTrigger SourceName="CCD" RoutedEvent="MouseDown">
                    <Setter Property="Popup.IsOpen" Value="True" />
                </EventTrigger>
            </Style.Triggers>
        </Style>
    </Popup.Style>
...

当事件发生在另一个控件上时,严格地在XAML中显示弹出窗口的最佳方法是什么?

What is the best way to show a popup strictly in XAML when an event happens on a different control?

推荐答案

我做了一些简单的操作,但是很有效.

I did something simple, but it works.

我使用了一个典型的ToggleButton,通过更改其控制模板将其重新设置为文本块.然后,我将ToggleButton的IsChecked属性绑定到弹出窗口的IsOpen属性.弹出窗口具有一些诸如StaysOpen的属性,可让您修改关闭行为.

I used a typical ToggleButton, which I restyled as a textblock by changing its control template. Then I just bound the IsChecked property on the ToggleButton to the IsOpen property on the popup. Popup has some properties like StaysOpen that let you modify the closing behavior.

以下内容在XamlPad中起作用.

The following works in XamlPad.

 <StackPanel>
  <ToggleButton Name="button"> 
    <ToggleButton.Template>
      <ControlTemplate TargetType="ToggleButton">
        <TextBlock>Click Me Here!!</TextBlock>
      </ControlTemplate>      
    </ToggleButton.Template>
  </ToggleButton>
  <Popup IsOpen="{Binding IsChecked, ElementName=button}" StaysOpen="False">
    <Border Background="LightYellow">
      <TextBlock>I'm the popup</TextBlock>
    </Border>
  </Popup> 
 </StackPanel>

这篇关于仅当使用XAML标记时,如何在单击另一个控件时打开WPF弹出窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆