在后面的代码中设置两个属性之间的绑定 [英] Set up a binding between two properties in code behind

查看:40
本文介绍了在后面的代码中设置两个属性之间的绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我看来,我有一个列表框,其中包含一些包含按钮的模板化项目.

In my view, I have a ListBox with some templated items that contain buttons.

<ListBox x:Name="MyListBox" ItemTemplate="{DynamicResource DataTemplate1}"
         ItemsSource="{Binding MyItems}">
</ListBox>

以及生成项目的模板:

<DataTemplate x:Key="DataTemplate1">
        <StackPanel Orientation="Horizontal">
            <Button Width="50" Click="Button_Click" />
        </StackPanel>
</DataTemplate>

当用户单击其中一个ListBox项上的按钮时,我想将该ListBox项的索引发送到我的ViewModel.

When user clicks a button on one of those ListBox items, I want to send the index of that ListBox item to my ViewModel.

因此想像在MVVM中那样使用Binding.但是我正在努力在两个属性之间的代码中建立绑定.

So figured to use Binding as it seems to be the way in MVVM. But I'm struggling to set up a binding in code between two properties.

我的查看代码如下:

public partial class ItemView : UserControl
{
    ViewModel.ItemViewModel VM;
    public ItemView()
    {
        InitializeComponent();
        VM = new ViewModel.ItemViewModel();
        this.DataContext = VM;
    }

    private int clickedItemIndex;
    public int ClickedItemIndex { get => clickedItemIndex; set => clickedItemIndex = value; }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        var ClickedItem = (sender as FrameworkElement).DataContext;
        ClickedItemIndex = MyListBox.Items.IndexOf(ClickedItem);
    }
}

我得到了索引并将其设置为 ClickedItemIndex 属性,
我的ViewModel中也有属性:

I get the index and set it to ClickedItemIndex property,
I also have property in my ViewModel:

public int SomeInt { get; set; }

现在如何设置这两个属性之间的绑定?

我对MVVM还是很陌生,仍然在学习它.因此,也许这不是正确的方法.但是我需要为每个单独的列表框项目提供一种方法,以便能够在更全局的viewmodel中调用效果.例如,如果我想进行删除"操作,在每个列表框项目上单击按钮,我将需要以某种方式将索引发送到viewmodel并使用index作为参数调用removeItem方法.还是有更好的方法来做类似的事情?

Now how do I set up a binding between these two properties?

I'm quite new to MVVM and still learning it. So, maybe this not the correct approach. But I need to have a way for each individual listbox item to be able to call upon an effect in more global viewmodel. For example, if I wanted to have a "Remove" button on each of the listbox items, I would somehow need to send the index to the viewmodel and call the removeItem method with index as the parameter. Or is there a better way to do similar things?

推荐答案

您无需使用 Button 即可选择项目.当您单击/点击该项目时,它将自动被选择.
然后只需将 ListBox.SelectedIndex 绑定到视图模型属性 SomeInt ,它将在每次选择时更新.

You don't need to use a Button in order to select the item. When you click/tap on the item it will get automatically selected.
Then simply bind ListBox.SelectedIndex to your view model property SomeInt and it will update on every selection.

WPF中的数据绑定概述

您还可以通过将 ListBox.SelectedItem 绑定到视图模型来获取商品本身.

You can also get the item itself by binding ListBox.SelectedItem to your view model.

您可以通过从属性的 set 方法调用处理程序来处理新值:

You can handle new values by invoking a handler from the property's set method:

ViewModel.cs

class ViewModel : INotifyPropertyChanged
{
  private int currentItemIndex;
  public int CurrentItemIndex
  {
    get => this.currentItemIndex;
    set
    {
      this.currentItemIndex = value;
      OnPropertyChanged();
     
      // Handle property changes
      OnCurrentItemIndexChanged();
    }
  }

  private MyItem currentItem;
  public MyItem CurrentItem
  {
    get => this.currentItem;
    set
    {
      this.currentItem = value;
      OnPropertyChanged();
    }
  }

  protected virtual void OnCurrentItemIndexChanged()
  {
    // Handle the new this.CurrentItemIndex value
  }

  // Implementation of INotifyPropertyChanged
  public event PropertyChangedEventHandler PropertyChanged;
  protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
  {
    this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  }
}

ItemView .xaml

<UserControl>
  <UserControl.DataContext>
    <ViewModel />
  </UserControl.DataContext>


  <ListBox ItemsSource="{Binding MyItems}" 
           SelectedIndex="{Binding CurrentItemIndex}"
           SelectedItem="{Binding CurrentItem}" />
</UserControl>

这篇关于在后面的代码中设置两个属性之间的绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆