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

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

问题描述

我有两个控件,一个 TextBlock 和一个 PopUp.当用户在文本块上单击 (MouseDown) 时,我想显示弹出窗口.我认为我可以使用弹出窗口上的 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 属性.Popup 有一些属性,如 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天全站免登陆