我在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内部
答案 0 :(得分:0)
我决定将Command="{Binding ClosePopUp}
替换为Command="{Binding DataContext.ClosePopUp, RelativeSource={RelativeSource AncestorType=UserControl}}"
按钮的数据上下文应与其父按钮相同。