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

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

问题描述

我有一个显示 MyObjects 集合的 ListBox.该集合位于 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.您可以在 Binding 中使用 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>

视图模型

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

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

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

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