gpt4 book ai didi

c# - 单击按钮时隐藏弹出按钮 (DelegateCommand)

转载 作者:行者123 更新时间:2023-12-03 10:52:10 26 4
gpt4 key购买 nike

我正在构建一个 Windows 应用商店应用程序,我有一个如下所示的弹出窗口:
flyout
Flyout运行正常,当我单击“添加”Button 时,我可以添加新地址.问题是,我希望能够隐藏 Flyout也是。我的 DelegateCommandViewModel ,所以我没有引用实际的 View元素。

我已尝试更改 DelegateCommand获取参数,如下所示:

public DelegateCommand<object> AddAddressCommand
{
get { return new DelegateCommand<object>(AddAddress, CanAddAddress); }
}

public void AddAddress(object parameter)
{
if (_isEditing)
{
NewAddress.PayeeId = CurrentPayee.Id;
_addressRepository.InsertAsync(NewAddress);
}
CurrentPayee.Addresses.Add(NewAddress);
NewAddress = new Address();

// TODO: hide Flyout
}

在我的 XAML 中,我尝试传递 CommandParameterDelegateCommand像这样:
<Button Content="Add" 
Command="{Binding AddAddressCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Self}}"
HorizontalAlignment="Center" />

如果我通过 {RelativeSource Self} ,我得到了 Button 的引用正如预期的那样,但我无法引用树上的任何其他内容。 “添加” ButtonFlyout 的 child ,附加到“添加地址” AppBarButton .

理想情况下,我可以传递对 Flyout 的引用。直接,或 AppBarButton , 这样我就可以调用 Flyout.Hide()当我单击“添加”按钮时。

我试过设置 x:NameAppBarButtonFlyout并在 CommandParameter 中引用它们像这样:
CommandParameter="{Binding ElementName=AddAddressFlyout}"

在这两种情况下,我的参数都是 null .是否可以使用直接数据绑定(bind)来完成此操作,还是我必须添加某种 Behavior年代?

最佳答案

也许我在您的要求中遗漏了一个微妙之处,但我能够使用 x:Name 和 .Hide() 轻松关闭弹出窗口

在您的 <Flyout> 中使用 x:Name="MyFlyout"标记,然后通过执行 MyFlyout.Hide() 在您的 C# 单击事件中将其关闭

XAML 代码:

<Page.BottomAppBar>
<CommandBar>
<AppBarButton Icon="Add">
<AppBarButton.Flyout>
<Flyout x:Name="MyFlyout">
<StackPanel>
<Button HorizontalAlignment="Right" Click="CancelButton_Click" Margin="0,-10,-10,0">
<SymbolIcon Symbol="Cancel"/>
</Button>
<TextBlock>Hello World</TextBlock>
<TextBox></TextBox>
</StackPanel>
</Flyout>
</AppBarButton.Flyout>
</AppBarButton>
</CommandBar>
</Page.BottomAppBar>

C# 代码
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}


private void CancelButton_Click(object sender, RoutedEventArgs e)
{
MyFlyout.Hide();
}
}

关于c# - 单击按钮时隐藏弹出按钮 (DelegateCommand),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28736005/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com