【问题标题】:Binding SelectedItem in comobox with current ItemSource?将组合框中的选定项目与当前的 ItemSsource 绑定?
【发布时间】:2017-02-03 13:36:56
【问题描述】:

所以我想要实现的是将当前项目从 ItemsSource 绑定到 Comobox 中的 SelectedItem 我希望下面的(一个简化的示例)代码能够展示我想要的。

public class book : INotifyPropertyChanged{
private string _title;
private string _author;

public book(){
    this.Title = "";
    this.
}
public string Title{
    get{return _title;}
    set{
        _title = value;
                NotifyPropertyChanged("Title");

    }
}
public string Author{
    get{return _author;}
    set{
        _author = value;
        NotifyPropertyChanged("Author");
    }
}

    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}

ThisViewModel.cs 代码:

public class ThisViewModel{
    private ObservableCollection<Book> _book;
    private List<Book> _existedBooks;
    public ObservableCollection<Book> Book{
        get{return _book;}
        set{_book = value;}
    }

    public ThisViewModel(){
        Books = new ObservableCollection<Book>();
        Books.Add(new Book())
    }
    public List<Book> ExistedBooks{
        get{return _existedBooks;}
        set{_existedBooks = value;}
    }
}

ThisView.xaml.cs 的代码隐藏:

public partial class ThisView{
    public ThisView(){
        InitializeComponent();
        this.DataContext = new ThisViewModel();

    }
}

XAML 代码 ThisView.xaml:

<ItemsControl ItemsSource="{Binding Path=Book}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <ComboBox  ItemsSource="{Binding Path=ExistedBooks}"  
            DisplayMemberPath="Title"
            SelectedItem="{Binding <HERE IS MY PROBLEM>}"
            ></ComboBox>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

如何将 selectedItem(现有书籍之一)绑定到 ItemsControl 当前项目(书籍)。

希望我的观点足够清楚。 感谢您的宝贵时间。

【问题讨论】:

标签: c# wpf xaml itemscontrol


【解决方案1】:

您应该将相同的 Book 对象添加到两个集合中才能正常工作:

public ThisViewModel()
{
    Book = new ObservableCollection<Book>();
    ExistedBooks = new List<Book>();

    Book bookA = new Book() { Title = "Title A" };
    Book.Add(bookA);
    ExistedBooks.Add(bookA);
}

然后您可以将 ComboBox 的 SelectedItem 属性绑定到 ItemsControl 中的当前项,如下所示:

<ItemsControl ItemsSource="{Binding Path=Book}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <ComboBox  ItemsSource="{Binding DataContext.ExistedBooks, RelativeSource={RelativeSource AncestorType=ItemsControl}}"
                               SelectedItem="{Binding Path=., Mode=OneWay}"
                               DisplayMemberPath="Title" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

【讨论】:

  • 但我真正需要将Books 集合中的Book 对象绑定(填充)到ExistedBooks 集合中的Book 对象。
  • 您不能将对象添加到 XAML 中的集合。您应该在视图模型中执行此操作。 XAML 是一种标记 语言。除此之外,应用程序逻辑应该在视图模型中实现。这是 MVVM 模式的基石之一。
  • 你回答说如果我在ExistedBooks 中有一本标题为 Title A 的书并且我选择了它,而Book 列表中的当前项目有标题Title B,如果我在这一点打破我的Title B 将变成Title A,或者这不可能吗?
  • 如前所述,您应该将相同的 Book 对象添加到两个集合中。仅仅因为两本书具有相同的标题,它们不会引用内存中的同一个 Book 对象。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-15
  • 2021-07-22
  • 1970-01-01
相关资源
最近更新 更多