如何使用列表框的项目中的绑定到ViewModel的属性 [英] How to use binding in the ListBox’s Items to the ViewModel’s properties

查看:100
本文介绍了如何使用列表框的项目中的绑定到ViewModel的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ListBox,它显示MyObjects的集合.该集合在ViewModel中.我想处理ListItem上的按钮单击,但是在绑定时遇到了一些麻烦.如果属性绑定到MyObject属性,则DataTemplate中的绑定可以正常工作.但是如何将其绑定到ViewModel的属性?

I have a ListBox which displays the collection of MyObjects. The collection is in the ViewModel. I want to handle a click on the button on the ListItem but have some troubles with binding. The binding in the DataTemplate works fine if the property is bound to the MyObject property. But how can I bind it to the property from the ViewModel?

第二个问题,我如何使用处理点击事件的代码中的项目信息.例如,我要从项目的TextBox中打印出文本.

The second question how I can use the information from the item in the code which handles click event. For instance, I want to print out the text from the item’s TextBox.

代码如下:

<Window.Resources>
    <DataTemplate x:Key="ItemTemplate">
        <Button Content="{Binding .}"
                Command="{Binding ClickCommand}" /> <!--It doesn't work-->
    </DataTemplate>

</Window.Resources>
<ListBox x:Name="ListBox"
         ItemsSource="{Binding Path=Objects}"
         IsSynchronizedWithCurrentItem="True"
         ItemTemplate="{StaticResource ItemTemplate}"/>

C#:

public partial class MainWindow : Window
{
    VM m_vm;

    public MainWindow()
    {
        m_vm = new VM();
        this.DataContext = m_vm;
        InitializeComponent();
    }
}

public class VM
{
    ObservableCollection<string> _objects;

    public ObservableCollection<string> Objects
    {
      get { return _objects; }
      set { _objects = value; }
    }

    public VM()
    {
        _objects = new ObservableCollection<string>();
        Objects.Add("A");
        Objects.Add("B");
        Objects.Add("C");
    }

    //I used relayCommand from the John Smith articles
    RelayCommand _clickCommand;
    public ICommand ClickCommand
    {
        get
        {
            if (_clickCommand == null)
            {
                _clickCommand = new RelayCommand(() => this.AvatarClick());
            }
            return _clickCommand;
        }
    }

    public void AvatarClick()
    {
        //how to get here the text from the particular item where the button was clicked?
    }
}

推荐答案

您的ListBoxItem将具有ObservableCollection对象中的字符串项作为DataContext,并且那里没有任何AvatarClick RelayCommand.您可以在绑定中使用RelativeSource来代替使用父ListBox中的DataContext.

Your ListBoxItem's will have the string items from the ObservableCollection Objects as DataContext and from there you don't have any AvatarClick RelayCommand. You can use RelativeSource in the Binding to use the DataContext from the parent ListBox instead.

对于第二个问题,您可以像这样使用CommandParameter

For your second question, you could make use of the CommandParameter like this

Xaml

<DataTemplate x:Key="ItemTemplate">
    <Button Content="{Binding .}"
            Command="{Binding DataContext.ClickCommand,
                              RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}"
            CommandParameter="{Binding .}"/>
</DataTemplate>

ViewModel

public ICommand ClickCommand
{
    get
    {
        if (_clickCommand == null)
        {
            _clickCommand = new RelayCommand(param => this.AvatarClick(param));
        }
        return _clickCommand;
    }
}

public void AvatarClick(object param)
{
    //...
}

这篇关于如何使用列表框的项目中的绑定到ViewModel的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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