【问题标题】:BInding for Listbox In WPFWPF中列表框的绑定
【发布时间】:2009-07-01 12:25:58
【问题描述】:

我有一个带文本框的主窗口。我输入一些文本并单击输入它会弹出一个带有列表框的窗口并显示与在主窗口中输入的文本匹配的项目。从 ListBox 中选择项目时,文本将设置在我的 Mainwindow 文本框中。

我遵循 MVVM 模式。我无法在主窗口中为列表框设置绑定。(使用 CommandBinding)

有人有类似场景的解决方案或示例吗?

谢谢

【问题讨论】:

    标签: wpf binding


    【解决方案1】:

    在 ViewModel 上,您需要一个属性 SelectedListBoxItem

    将其绑定到 ListBox 的 SelectedItem

    将 Text-property 绑定到 ViewModel 的 SelectedListBoxItem

    基本上应该就是这样。

    <ListBox
            ItemsSource="{Binding Path=ItemsView, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, Mode=TwoWay}"    
            SelectedItem="{Binding Path=SelectedListBoxItem, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, Mode=TwoWay}"
            />
    
    <TextBox Text="{Binding Path=SelectedListBoxItem, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, Mode=TwoWay}"
            />
    

    【讨论】:

    • 我正在尝试类似的东西,但它对我不起作用。可以贴一下示例代码吗?
    猜你喜欢
    • 2010-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-28
    • 1970-01-01
    • 2010-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多