【问题标题】:WPF - Bind a Control to a propertyWPF - 将控件绑定到属性
【发布时间】:2013-06-14 13:49:34
【问题描述】:

一切都在标题中,我想做类似的事情:

<ListBox AssignTo="{Binding ListBoxControl}" ...

然后在我的 ViewModel 中具有这样的属性:

public ListBox CurrentListBox 
{
    get; 
    set;
}

我想要的是轻松检索所选项目

我可以这样做吗?

【问题讨论】:

  • 如果你使用 MVVM 模式,你的 ViewModel 不知道你的 View,所以你不应该在那里有 View 相关的属性,就像在这种情况下 ListBox。据我了解,您的虚拟机应该只公开可以绑定到视图的模型相关属性。
  • 视图模型不应该在控件上操作
  • 你能做什么?您编写了一个绑定到ListBoxControl 的绑定,然后不在任何地方定义ListBoxControl。如果您打算绑定到CurrentListBox,那么您很可能想要使用ContentControl 代替:&lt;ConentControl Content="{Binding CurrentListBox}" /&gt;,但我建议不要这样做。
  • 我想更改用于启动集成测试的 gui。我目前在 SelectedItem 上有一个绑定,但我希望能够同时选择多个。我不想让代码变得更加难以阅读,因为他现在正在做类似的事情:here
  • 为什么不将集合中的项目包装在具有 isSelected 属性的容器中,然后绑定到该容器?

标签: c# wpf


【解决方案1】:

你的 xaml 应该看起来像

    <ListBox
        ItemsSource="{Binding List}"
        SelectedItem="{Binding Item}"/>

你的属性应该看起来像

    private List<yourClass> list;
    public List<yourClass> List
    {
        get { return list; }
        set
        {
            list = value;
            RaisPropertyChanged("List");
        }
    }

    private yourClass item;
    public yourClass Item
    {
        get { return item; }
        set
        {
            item = value;
            RaisPropertyChanged("Item");
        }
    }

现在您只需将 ViewModle 设置为 DataContext 并且有很多方法可以做到这一点

我使用一种非常简单的方法来创建一个 ResourceDictionary (VMViews.xaml)

<ResourceDictionary  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

                     xmlns:vm="clr-namespace:ProjectName.namespace"
                     xmlns:vw="clr-namespace:ProjectName.namespace" >

    <DataTemplate DataType="{x:Type vm:YourVMName}">
        <vw:YourVName/>
    </DataTemplate>
</ResourceDictionary>

在我的 App.xaml 中

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/Path/VMViews.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

编辑所选项目

(此代码未经测试)

XAML

<ListBox
    ItemsSource="{Binding List}"
    SelectedItem="{Binding Item}" SelectionChanged="ListBox_SelectionChanged">
    <ListBox.Resources>
        <Style TargetType="{x:Type ListBoxItem}">
            <Setter Property="IsSelected" Value="{Binding IsSelected}" />
        </Style>
    </ListBox.Resources>
</ListBox>

现在你的yourClass 需要实现IsSelected 属性

你可以做类似的事情

private yourClass item;
    public yourClass Item
    {
        get { return item; }
        set
        {
            item = value;
            RaisPropertyChanged("Item");

            SelectedItems = List.Where(listItem => listItem.IsSelected).ToList();
        }
    }

    private List<yourClass> selectedItems;
    public List<yourClass> SelectedItems
    {
        get { return selectedItems; }
        set
        {
            selectedItems = value;
            RaisPropertyChanged("SelectedItems");
        }
    }

【讨论】:

  • 哎呀......你有“价值”,而不是“价值”:)
  • 我想要多个选定的项目,而不仅仅是一个! SelectedItems="{Binding SelectedTest}" SelectionMode="Extended"/&gt;
  • 顺便说一句工作正常,为什么Multiple这么痛苦?
  • 哦,对不起@Thomas 我以为只有一个,所以你需要一种行为,因为你不能直接绑定到 SelectedItems AFAIK
  • @Thomas 看到我的编辑可能会对您有所帮助
【解决方案2】:

另一个人在尝试学习时遇到了类似的问题...

Take a look at this answer to help

您的属性通过公共 getter/setter 公开(尽管 setter 可以是您的数据上下文对象私有的)。

然后,列表框“Items”的属性将“绑定”到您公开的属性。

【讨论】:

  • 我需要访问 selectedItems,当我尝试使用经典绑定时,我有:'SelectedItems' 属性是只读的,不能从标记中设置。
  • @Thomas,然后我会在 WiiMaxx 采样时扩展您模型上的属性,但是您需要一个项目列表来处理多个项目选择,而不是单个“项目”......但是原理还是一样的。
  • 很抱歉,在阅读了 5 次您引用的帖子和您的答案后,我没有看到有关如何检索用户选择的项目的链接。
  • @Thomas,点击“看看这个答案来帮助你”的超链接文本......着色是一个糟糕的选择,几乎看起来与普通文本相同。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-06
  • 2018-04-09
  • 2019-07-11
相关资源
最近更新 更多