WPF项目控制绑定问题

时间:2012-02-21 11:04:54

标签: wpf vb.net binding tabcontrol

<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是一个readOnly属性所以我不能将它定义为Mode =“TwoWay”..我看一下调试和属性ExportFormat获取新项目的更新,但usercontrol仍然显示第一个选项! !

真诚的Rui Nunes

2 个答案:

答案 0 :(得分:1)

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

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

  • 如果它实际上是ObservableCollection,则直接指定它,而不是清除其项目并添加新项目。例如:

    ExportFormat = MyNewObsCollection; //Bad

ExportFormat.Clear();

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

附注:ExportFormat应为readonly

答案 1 :(得分:0)

感谢@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

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

相关问题