首页 > 解决方案 > 将命令/参数传递给 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);
    }

标签: c#wpfmvvmparameters

解决方案


您可以使用 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"/>

当您使用绑定时,将您的代码最小化会更加美观。


推荐阅读