【问题标题】:WPF itemscontrol binding problemsWPF项目控件绑定问题
【发布时间】:2012-02-21 11:04:54
【问题描述】:
<ItemsControl ItemsSource="{Binding ExportFormat, UpdateSourceTrigger=PropertyChanged}" Grid.Column="1">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Button Content="{Binding}" Margin="5" Height="50" Width="70" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.setExportFormat, UpdateSourceTrigger=PropertyChanged}" CommandParameter="{Binding}"></Button>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>

所以我在我的 xaml 中有该代码,并且该按钮被归档为字符串列表。根据用户在以前的 Usercontrol 上选择的内容,该项目将与不同的项目一起归档。问题是如果用户在第一次运行时选择一个选项,按钮将被正确填充,但如果用户返回并选择另一个选项,控件不会更新并显示与以前相同.. 我的英语不是最好的,但我想我可以让我理解!任何的想法?! PS:Button 上的 bindind 是只读属性,因此我无法将其定义为 Mode="TwoWay".. 我查看了调试,属性 ExportFormat 使用新项目获取更新,但用户控件仍显示第一个选项! !

真诚的 Rui Nunes

【问题讨论】:

  • ViewModel 上的 ExportFormat 类型是什么?
  • 正如狒狒指出的那样——这是你的问题——ObservableCollection 通知订阅者(绑定)发生了一些变化——List 没有。
  • 请不要将 SOLVED 编辑到标题中或将您的解决方案编辑到问题正文中。发布解决方案作为答案。
  • @ChrisF 好的,我会尽快发布解决方案,因为我没有 100 个代表,我必须等待至少 6 个小时才能回答我自己的问题!

标签: wpf vb.net binding tabcontrol


【解决方案1】:

你没有提供代码隐藏,所以我要在这里在黑暗中拍几张照片:

  • ExportFormatcollection 不是 ObservableCollection(或更一般地说,不实现 INotifyCollectionChanged)。

  • 如果它实际上是一个 ObservableCollection,则直接分配它,而不是清除它的项目并添加新的项目。示例:

    ExportFormat = MyNewObsCollection; //Bad

ExportFormat.Clear();

foreach(var newItem in myNewObsCollection)
{
   ExportFormat.Add(newItem); //Good
}

附注:ExportFormat 应该是只读的

【讨论】:

  • 好的,所以我解决了我的问题,只需将 ExportFormat 的类型更改为 ObjectModel.ObservableCollection(Of String) 它之前是 List(of String) .. 有点奇怪为什么它不能与 List 和与 ObservableCollection 合作!
  • 其实这就是它的本意:列表不提供任何内置于 WPF 的更新机制。每当您修改集合时,ObservableCollection 都会引发 CollectionChanged,并且 WPF 订阅该事件。
【解决方案2】:

感谢@Baboon 让我了解了这个问题。所以我的问题的解决方案是:

所以我的 ExportFormat 属性被定义为:

Private _ExportFormat As New List(Of String)
Public Property ExportFormat As List(Of String)
Get
Return _ExportFormat
End Get
Set(value As List(Of String))
_ExportFormat = value
NotifyPropertyChanged("ExportFormat")
End Set
End Property

我只需要将 List(of String) 更改为 ObjectModel.ObservableCollection(Of String)..

Private _ExportFormat As New ObjectModel.ObservableCollection(Of String)
Public Property ExportFormat As ObjectModel.ObservableCollection(Of String)
Get
        Return _ExportFormat
End Get
Set(value As ObjectModel.ObservableCollection(Of String))
_ExportFormat = value
NotifyPropertyChanged("ExportFormat")
End Set
End Property

我的问题得到了解决..再次感谢!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-09
    • 2011-04-07
    • 2010-12-15
    • 1970-01-01
    相关资源
    最近更新 更多