【问题标题】:Datepicker passing parameter through command parameter wpf mvvmDatepicker 通过命令参数 wpf mvvm 传递参数
【发布时间】:2023-03-19 23:16:02
【问题描述】:

我有一个使用 Prism 的 WPF MVVM 项目,我有一个绑定到视图模型上的 Datetime 属性的 Datepicker。我想使用SelectedDateChanged 事件来获取更改后的值(后面没有代码)。我知道我也得到了Datepicker 绑定的属性的值变化,但我想以另一种方式尝试。到目前为止,我已经将日期选择器放在了 xaml 中

<DatePicker Name="dpName" Grid.Row="1" Grid.Column="4" VerticalAlignment="Center" HorizontalAlignment="Center" Width="200" SelectedDate="{Binding Class.Date}">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="SelectedDateChanged">
                        <i:InvokeCommandAction Command="{Binding ath=TestMeCommand}" />
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </DatePicker>

TestMeCommand 的 C# 代码:

public DelegateCommand TestMeCommand { get; set; }

在 ctor 中:

TestDateCommand = new DelegateCommand<object>(TestPateparam);

功能:

 private void TestDate()
    {
        int test = 0;
    }

我的问题是如何将选定的日期作为参数传递给函数。到目前为止,我已经尝试将InvokeCommandAction 替换为

<i:InvokeCommandAction Command="{Binding Path=TestDateCommand}" CommandParameter="Binding ElementName=DPLoanDetailsAsOfDate, Path=SelectedDate}"  />

和c#函数

  public DelegateCommand<object> TestDateCommand { get; set; }
  TestDateCommand = new DelegateCommand<object>(TestPateparam);

      private void TestPateparam(object dt)
    {
        int test = 0;
    }

【问题讨论】:

  • 您的命令参数绑定缺少前导 {。不确定这是否是问题所在。如果您使用命令来执行验证,那么您做错了。使用 INotifyDataErrorInfo 进行验证。
  • 调试器到TestPateparam()了吗?如果有,看看dt 有什么。
  • 您不能使用 InvokeCommandAction。参考stackoverflow.com/questions/20659070/…
  • setup, Ayyappan Subramanian 调试器确实转到了 TestDateParam 但我没有得到日期作为参数我得到“Binding ElementName=datePickerName, Path=SelectedDate}”,我在这种情况下将参数设置为对象不是日期时间

标签: c# wpf mvvm prism


【解决方案1】:

如果您使用的是 Prism 6.1.1-pre2,您可以使用 Prism 的 InvokeCommandAction 轻松完成此操作:

    <DatePicker  Grid.Row="1" Margin="5" >
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="SelectedDateChanged">
                <prism:InvokeCommandAction Command="{Binding SelectedCommand}" TriggerParameterPath="AddedItems" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </DatePicker>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    • 2014-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-29
    相关资源
    最近更新 更多