c# - 将命令/参数传递给 BackCode?
问题描述
自学并为可能是一个基本概念的东西而苦苦挣扎。所以欢迎任何额外的解释......我有以下代码可以完美地从 Button_Click 事件和代码后面工作。我可以使用一点代码隐藏,因为它基本上只是在操纵视图。但是,我想从 ViewModel 中获取数据/业务逻辑。我能够确认(使用 messagebox.show)我的数据已正确传递,但是 UI 不会像数据来自 Button_Click 事件时那样使用新数据进行更新。一段时间以来,我一直很难解决这个问题,并一直在寻找解决方法。据我所知,我的方法是完全错误的。我真的很想知道如何做到这一点。
WPF
<StackPanel Grid.Row="1" VerticalAlignment="Bottom">
<TextBox x:Name="NumberOfDays" HorizontalAlignment="Left" Background="GreenYellow" Foreground="Black">
</TextBox>
<StackPanel Orientation="Horizontal">
<Button Content="change hello row" Click="Button_Click" HorizontalAlignment="Left" Background="GreenYellow" Foreground="Black">
</Button>
<Button Content="TestCommand5" Margin="0 0 0 0" Padding="5" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" FontSize="16"
CommandParameter="{Binding objActiveJobClass.JobID}"
Command="{Binding TestCommand5}"
IsTabStop = "False" FocusVisualStyle="{DynamicResource MyFocusVisual}"
Style="{Binding Mode=OneWay, Source={StaticResource NavigationButtonStyle}}">
</Button>
</StackPanel>
<Label Content="Gant Chart Area" HorizontalAlignment="Left"/>
<ScrollViewer Width="1200" HorizontalAlignment="Left" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
<Grid >
<Canvas x:Name="GantChartCanvas" HorizontalAlignment="Left" Background="Yellow" Height="405" />
</Grid>
</ScrollViewer>
</StackPanel>
回码:
public partial class GantChartUserControl : UserControl
{
public GantChartUserControl()
{
InitializeComponent();
}
public GantChartUserControl(int Duration)
{
InitializeComponent();
CreateTimeLine(Duration);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
int variable = 0;
if (NumberOfDays != null)
{
int.TryParse(NumberOfDays.Text, out variable);
if (variable > 0)
{
CreateTimeLine(variable);
}
else
{
MessageBox.Show($"\"{NumberOfDays.Text}\" is not an INTEGER greater than Zero.");
}
}
}
public void CreateTimeLine(int duration)
{
MessageBox.Show($"CreateTimeLine duration {duration}");
GantChartCanvas.Children.Clear();
double ControlWidth = 100d;
int Duration = duration;
for (int i = 0; i < Duration; i++)
{
Label u1 = new Label();
u1.HorizontalAlignment = HorizontalAlignment.Left;
u1.VerticalAlignment = VerticalAlignment.Bottom;
u1.HorizontalContentAlignment = HorizontalAlignment.Center;
u1.VerticalContentAlignment = VerticalAlignment.Center;
u1.Background = IsOdd(i) ? Brushes.Gray : Brushes.DarkGray;
u1.Height = 30;
u1.Width = ControlWidth;
u1.SetValue(Canvas.LeftProperty, (ControlWidth * i));
u1.SetValue(Canvas.BottomProperty, 0d);
u1.Content = string.Concat("LABEL ", i + 1);
GantChartCanvas.Width = Duration * ControlWidth;
GantChartCanvas.Children.Add(u1);
}
}
public static bool IsOdd(int value)
{
return value % 2 != 0;
}
}
视图模型
private ICommand _TestCommand5;
public ICommand TestCommand5
{
get
{
if (_TestCommand5 == null)
{
_TestCommand5 = new RelayCommand<object>(ExecuteTestCommand5, CanExecuteTestCommand5);
}
return _TestCommand5;
}
}
public bool CanExecuteTestCommand5(object parameter)
{
return true;
}
public void ExecuteTestCommand5(object parameter)
{
Debug.WriteLine($"\nDataBaseHelperClass: {System.Reflection.MethodBase.GetCurrentMethod()}");
int testint = 44;
GantChartUserControl objGantChartUserControl = new GantChartUserControl();
objGantChartUserControl.CreateTimeLine(testint);
}
解决方案
您可以使用 MVVMLight 框架,它简化了您的绑定。使用此框架,您只需创建一个属性public RelayCommand YourButton{ get; set; }
在 ViewModel 的构造函数中,您只需添加这一行
YourButton = new RelayCommand(Method); // Method is the method to call when you click on the button
之后,您只需在属性上绑定您的按钮。
一个小例子:
视图模型:
public RelayCommand MyButton { get; set; }
public MainViewModel()
{
MyButton = new RelayCommand(Action);
}
public void Action(){
Console.Writeline("Button Pressed");
}
WPF
<Button Command="{Binding MyButton}" Content="Button"/>
当您使用绑定时,将您的代码最小化会更加美观。
推荐阅读
- java - 如何将 AppendBlob/大于 4mb 限制的文件上传到 Java 中的 Azure 存储/Blob?
- azure - Azure DevOps 通用包 - 获取从外部源下载的工件版本
- google-cloud-platform - Gsutil 错误“捕获套接字错误,重试:超时”
- linux-kernel - #address-cells = <2> 情况的 DTC 编译错误
- debugging - Neovim 上的工作 pwa-chrome 类型启动 vimspector 配置是什么?
- php - 从数据库中获取值作为表
- arrays - 如何根据vue js中的数组唯一ID在选择选项中显示数据
- r - 逻辑回归输出建议
- node.js - 通过基于 nodejs 的微服务处理大量并行请求以修改 nosql 数据库中的许可证计数
- c++ - 编辑基于 Llvm 的项目时,Clang 代码模型在 qtcreator 中停止工作