【问题标题】:Can I Pass by Reference in XAML ICommand我可以在 XAML ICommand 中通过引用传递吗
【发布时间】:2015-09-15 00:50:43
【问题描述】:

在这段代码中:

        <ListBox 
            x:Name="DataList1"
            xmlns:local="clr-namespace:xaml_binding_commands"
            >
            <ListBox.Resources>
                <local:CommandUp x:Key="CommandUp1"></local:CommandUp>
            </ListBox.Resources>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                    <TextBox
                        Text="{Binding Height,Mode=TwoWay,StringFormat=00\{0:d\}}"
                        InputScope="Number"/>

                    <RepeatButton
                        Content="+"
                        Command="{StaticResource CommandUp1}"
                        CommandParameter="{Binding }"
                        />

                    <TextBox
                        Text="{Binding Weight,Mode=TwoWay}"
                        InputScope="Number"/>

                    <RepeatButton
                        Content="+"
                        Command="{StaticResource CommandUp1}"
                        CommandParameter="{Binding Weight}"
                        />

                    </StackPanel>

                </DataTemplate>
            </ListBox.ItemTemplate>

        </ListBox>

还有这个

namespace xaml_binding_commands
{
    public class CommandUp : ICommand
    {
        public bool CanExecute(object parameter)
        {
            return true;
        }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
        if (parameter is Entry)
        {
            Entry EntryToUp = (Entry)parameter;
            EntryToUp.Height +=1; // no worky, which field to increment?!
        }
        if (parameter is Int32)
        {
            Int32 EntryFieldToUp = (Int32)parameter;
            EntryFieldToUp += 1; // no worky
        }
    }
}

public class Entry : INotifyPropertyChanged
{

    private Int32 _Height;

    public Int32 Height
    {
        get { return _Height; }
        set { _Height = value; PropChange("Height"); }
    }

    private Int32 _Weight;

    public Int32 Weight
    {
        get { return _Weight; }
        set { _Weight = value; PropChange("Weight"); }
    }

    private void PropChange(String PropName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(PropName));
    }

    public event PropertyChangedEventHandler PropertyChanged;

}



public partial class MainPage : PhoneApplicationPage
{
    public MainPage()
    {
        InitializeComponent();
        this.Loaded += MainPage_Loaded;
    }

    private ObservableCollection<Entry> _People = new ObservableCollection<Entry>();

    public ObservableCollection<Entry> People
    {
        get { return _People; }
        set { _People = value; }
    }

    void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        DataList1.ItemsSource = People;
        People.Add( new Entry() { Height=67, Weight=118 } );

    }
}

}

我可以通过引用传递文本框绑定的字段吗?如果我将整个类(一个条目)传递给 CommandParameter,以便它可以操作和递增,我无法知道是哪一组 TextBoxes 和 Buttons 导致了 Command.Execute。如果我传递与 TextBox 绑定的相同内容,即单独传递:Weight,Height,则 Command.Execute 无法影响输入。通常我会 PassByReference 并且我的 Int32 会被装箱,而我的一般操作函数可以对 by-ref 参数进行操作。我可以在 XAML 中以某种方式执行此操作吗?

【问题讨论】:

  • 您可以使用绑定属性的设置器。可选用 UpdateSrcTrigger=PropertyChanged。
  • @HenkHolterman 使用 setter 做什么?我已经在通知场景中使用了设置器,并且该部分工作正常。问题出在 ICommand 中。我也不清楚 UpdateSourceTrigger 将如何影响我的 ICommand 问题。

标签: c# wpf xaml data-binding pass-by-reference


【解决方案1】:

如果我这样做,我会使用 MVVM Light 和 RelayCommand&lt;string&gt;。这意味着您可以传入一个(或多个)参数作为绑定到ICommand 的一部分。

这意味着您可以对附加到按钮的单个事件处理程序进行多个绑定,并且每个绑定可以具有不同的参数,让您知道它的来源。

更新 1

MVVM Light 是一个 MVVM 库,几乎兼容所有东西,从标准 WPF 到 Windows 8.1 再到 Windows phone。见http://www.mvvmlight.net/。根据 NuGet 下载统计数据,它可能是最受欢迎的 MVVM 库,也是我更喜欢的库。

有关如何将 MVVM 灯与CommandParameter 一起使用的示例,请参阅MVVM Light RelayCommand Parameters 的最高投票答案。

有关如何将两个或多个参数传递给RelayCommand 的示例,请参阅How to Passing multiple parameters RelayCommand?

更新 2

看看你的代码,我会使用 MVVM。我通常更喜欢 MVVM 来编写代码(这本身就是一个完整的讨论)。如果您将所有数据放在 ViewModel 中,并使用绑定让您的 XAML 视图更新 ViewModel,我认为开发和维护会变得容易得多。

【讨论】:

  • 在这里给我一些上下文:MultiBinding 类似于 RelayCommand?和 MVVM Light 与哪一代 WPF XAML 兼容?是图书馆吗?
  • 是的,这是大方向。另一种选择是带有附加消息的 Caliburn.Micro。
  • @HenkHolterman 谢谢,如果我的想法有你的投票,那么他们很可能走在正确的轨道上。
  • 所以,要具体回答我的问题,您可能无法完全按照我的意愿进行操作。所以在更高的层次上,我真正想做的是一次通过几件事。很好 - 在这方面答案看起来不错。您能否详细说明我实际所做的任何特定差异 - 使用 XAML 标记作为视图...使用 Entry 和 People 作为模型,并使用 ICommand 和 NotifyPropertyChanged 作为各种控制器......你能详细说明一下区别,这与你建议的“将所有数据放入 ViewModel”,“使用绑定让 XAML View 更新 ViewModel”
  • 关键是当我实际上已经遵循其中表达的设计模式并获得所有好处时,我是否需要开始使用一些官方的“框架工具包”。所有的好处,除了可能有一些额外的附加连接的能力,等等​​。
猜你喜欢
  • 2014-08-22
  • 2020-09-08
  • 1970-01-01
  • 1970-01-01
  • 2011-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多