弹出框内的按钮命令,绑定不起作用
Button Command inside a popupbox, binding not working
我在 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) 时,该命令将不起作用。我认为这与绑定有关。 popubox 外的每个按钮都可以正常工作。
知道哪里出了问题吗?
编辑
PopupBox 在 DataTemplate 中
我通过将 Command="{Binding ClosePopUp}
替换为 Command="{Binding DataContext.ClosePopUp, RelativeSource={RelativeSource AncestorType=UserControl}}"
来解决
按钮的数据上下文应与其父按钮相同。
我在 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) 时,该命令将不起作用。我认为这与绑定有关。 popubox 外的每个按钮都可以正常工作。
知道哪里出了问题吗?
编辑
PopupBox 在 DataTemplate 中
我通过将 Command="{Binding ClosePopUp}
替换为 Command="{Binding DataContext.ClosePopUp, RelativeSource={RelativeSource AncestorType=UserControl}}"
按钮的数据上下文应与其父按钮相同。