切换弹出内容wpf

时间:2012-06-18 11:34:23

标签: wpf xaml popup toggle

这是我想要做的。

切换按钮1(editNameOpen)

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

切换按钮2(editNameClose)

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

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

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

    <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>
    

1 个答案:

答案 0 :(得分:2)

我个人会将Popup.IsOpenToggleButtons绑定到DataContext

中的单个布尔属性

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

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

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

如果你真的想这样做,而不是使用DataContext中的属性,请尝试绑定IsOpen的{​​{1}}属性和Popup属性你的IsChecked一起。

ToggleButtons