【问题标题】:Button Command inside a popupbox, binding not working弹出框中的按钮命令,绑定不起作用
【发布时间】:2019-11-13 00:00:53
【问题描述】:

我在 wpf 的 PopuBox 中有一个按钮 (x:name="MyButton")。

<materialDesign:PopupBox PlacementMode="BottomAndAlignRightEdges" 
                         StaysOpen="True"  
                         Padding="0 0 -1 0"
                         VerticalAlignment="Center">
        <materialDesign:PopupBox.ToggleContent>
                <materialDesign:PackIcon Kind="Calendar" 
                                         Foreground="{StaticResource PrimaryHueLightBrush}"
                                         Width="20" 
                                         Height="20"
                                         VerticalAlignment="Center"/>
                      </materialDesign:PopupBox.ToggleContent>
                              <Button Content="Ok"
                                      x:Name ="MyButton"
                                      Style="{DynamicResource MaterialDesignFlatButton}"
                                      Margin="0 5 40 5"
                                      Command="{Binding ClosePopUp}"/>                                    
</materialDesign:PopupBox>

问题是当我单击按钮 (MyButton) 时,该命令将不起作用。我认为这与绑定有关。弹出框外的每个按钮都可以正常工作。

知道有什么问题吗?

编辑

PopupBox 位于 DataTemplate 中

【问题讨论】:

    标签: c# wpf data-binding command


    【解决方案1】:

    我通过将Command="{Binding ClosePopUp} 替换为Command="{Binding DataContext.ClosePopUp, RelativeSource={RelativeSource AncestorType=UserControl}}" 解决了

    按钮的数据上下文应该等于它的父级。

    【讨论】:

    • 当命令是静态而不是动态绑定时如何?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-09
    • 1970-01-01
    • 2020-03-06
    • 2020-05-28
    • 1970-01-01
    • 2018-07-31
    • 2019-05-20
    相关资源
    最近更新 更多