gpt4 book ai didi

c# - 如何在代码隐藏中绑定(bind)命令以在 WPF 中查看?

转载 作者:太空宇宙 更新时间:2023-11-03 12:38:11 24 4
gpt4 key购买 nike

我想通过我 View (GenericReportingView.xaml) 中的按钮在我的代码隐藏 (GenericReportingView.xaml.cs) 中执行命令。

GenericReportingView.xaml:

 <Grid  Grid.Row="0" Grid.Column="0">
<Button Content="GetReport" Command="{Binding GetReportCommand}" HorizontalAlignment="Left" Width="50" />
</Grid>

通用报告 View .xaml.cs:

public partial class GenericReportingView
{
private DelegateCommand _getReportCommand;
public DelegateCommand GetReportCommand
{
get { return _getReportCommand ?? (_getReportCommand = new DelegateCommand(GetReport, (obj) => true)); }
}

public GenericReportingView()
{
InitializeComponent();
}

public void GetReport(object obj)
{
//Do something..
}
}

但是命令没有被调用..任何帮助将不胜感激。

提前致谢。

最佳答案

您不应绑定(bind)到代码隐藏中的属性。绑定(bind)通常用于将控件链接到 View 模型中的属性(在这种情况下看起来您没有 View 模型)。相反,您可以只使用按钮上的点击处理程序来调用您的方法:

GenericReportView.xaml:

<Grid  Grid.Row="0" Grid.Column="0">
<Button Content="GetReport" Click="GetReport" HorizontalAlignment="Left" Width="50" />
</Grid>

GenericReportView.xaml.cs

public partial class GenericReportingView
{
public GenericReportingView()
{
InitializeComponent();
}

public void GetReport(object obj, RoutedEventArgs e)
{
//Do something..
}
}

关于c# - 如何在代码隐藏中绑定(bind)命令以在 WPF 中查看?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40110705/

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