【问题标题】:toggling the popup content wpf切换弹出内容 wpf
【发布时间】:2012-06-18 11:34:23
【问题描述】:

这就是我想要做的。

切换按钮 1 (editNameOpen)

  • 选中后,显示弹出内容
  • 关闭它的唯一方法是使用弹出窗口内的另一个切换按钮 (editNameClose)

切换按钮 2 (editNameClose)

  • 弹出窗口内的这个
  • 当 IsChecked 时,关闭弹出窗口并关闭 editNameOpen

这是我用来尝试解决问题的一些 xaml。到目前为止的问题/疑问:

  1. 我的 MultiBinding 错误并引发运行时错误“无法设置 MultiBinding,因为必须指定 MultiValueConverter。”在这种情况下,MultiValueConverter 会转换什么?
  2. 在选中第二次时,我将如何关闭第1次切换按钮?

干杯,
浆果

<ToggleButton x:Name="editNameOpen" Style="{StaticResource EditToggleButtonStyle}" Grid.Column="1" Grid.Row="0"/>

<Popup x:Name="popupNameEditingControl"
       PlacementTarget="{Binding ElementName=editeditNameOpenName}"
       PopupAnimation="Slide"
       StaysOpen="False"    ** shoulf this be true?
       MinWidth="50">

    ** open and stay open while until editNameClose is checked
    <Popup.IsOpen>
        <MultiBinding >
            <Binding Mode="OneWay" ElementName="editNameOpen" Path="IsChecked"/>
            <Binding Mode="OneWay" ElementName="editNameClose" Path="IsChecked" Converter="{StaticResource invertBoolConv}"/>
        </MultiBinding>
    </Popup.IsOpen>

    ** how do we reset editNameOpen to be NOT IsChecked when editNameClose is checked?
    ** how do we reset editNameClose to be NOT IsChecked and then reset editNameClose to also be not checked when this opens again?
    <StackPanel Orientation="Horizontal" Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}">
        <Label Content="Hello Wolrd!"/>
        <ToggleButton x:Name="editNameClose" Content="X"/>
    </StackPanel>
</Popup>

【问题讨论】:

    标签: wpf xaml popup toggle


    【解决方案1】:

    我个人会将Popup.IsOpenToggleButtons 绑定到DataContext 中的单个布尔属性

    因此,当第一个ToggleButton 被检查时,它将布尔值设置为true,这使得Popup.IsOpen 评估为true 并打开Popup

    第二个ToggleButton 可能需要使用Converter 来反转布尔属性,所以当IsOpen = true 时显示为未选中,选中它将变为IsOpen = false,这将自动关闭Popup并取消选中第一个ToggleButton

    至于您遇到的错误,MultiBinding 需要 Converter 类型为 IMultiValueConverter,因为您不能将一个属性绑定到两个单独的值。您需要一个转换器将这些值转换为您可以使用的单个值。

    如果您真的想这样做而不是使用DataContext 中的属性,请尝试将PopupIsOpen 属性和ToggleButtonsIsChecked 属性绑定在一起。

    <Popup x:Name="popupNameEditingControl"
           IsOpen="{Binding IsChecked, ElementName=editNameOpen, Mode=TwoWay}"
           ... >
        ...
        <ToggleButton x:Name="editNameClose" Content="X"
                      IsChecked="{Binding IsChecked, ElementName=editNameOpen, 
                          Converter={StaticResource ReverseBooleanConverter}, Mode=TwoWay}" />
        ...
    </Popup>
    

    【讨论】:

    • 我最初是使用 vm 来控制它,然后决定这真的是一个视图问题。虽然它确实听起来更简单,但我将首先通过仅视图方法至少作为练习来工作。干杯
    • 如果您对 Popup.Style 的外观有一个概念,您会很乐意看到它!
    • @Berryl 我个人讨厌 WPF 的内置 Popup 控件,通常使用我自己的自定义 UserControl 代替。有兴趣的可以找代码here
    • @Berryl 我还添加了一个代码示例,用于将IsOpenIsChecked 属性绑定到我的答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-14
    • 1970-01-01
    • 1970-01-01
    • 2014-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多