2

我有一个静态 IList,它充当静态类中的存储库:

//静态类设置

public static IList RecentSearchedRepo = new ObservableCollection<object>();

和一个位于另一个类中的 IList,我将我的 UI 网格绑定到:

//组件类

private IList recentsearch = new ObservableCollection<object>();
public IList RecentSearch
{
    get
    {
        return recentsearch;
    }
    set
    {
        recentsearch = value;
        RaisePropertyChanged("RecentSearch");
    }
}

我将对象添加到 RecentSearchedRepo :

RecentSearchedRepo.add(searchitem)

然后将RecentSearch设置为静态列表

RecentSearch = Settings.RecentSearchedRepo;

XAML 片段:

<GridLayout:RecentSearchGrid x:Name="recentSearchGrid" ItemsSource="{Binding RecentSearch}" />

扩展 UserControl 的 RecentSearchGrid 类的片段:

 public IList ItemsSource
 {
     get
     {
         return GetValue(ItemsSourceProperty) as IList;
     }
     set
     {
         SetValue(ItemsSourceProperty, value);
     }
}

private static readonly DependencyProperty ItemsSourceProperty =
                    DependencyProperty.Register("ItemsSource", typeof(IList), typeof(RecentSearchGrid), new PropertyMetadata(null, OnItemsSourcePropertyChanged));


private static void OnItemsSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
     RecentSearchGrid source = d as RecentSearchGrid;

     if (source != null)
     {
      source.setListforgrid(source.ItemsSource);
     }
}

问题是当我将第一项添加到RecentSearchedRepoUI 时,UI 已更新,但在随后的每次添加中,UI 都不会更新。

4

3 回答 3

1

代替:

RecentSearch = Settings.RecentSearchedRepo;

尝试做:

RecentSearch.Clear();
var freshData = Settings.RecentSearchedRepo;
if (freshData != null)
    foreach (var item in freshData)
        RecentSearch.Add(item);

您通过重新分配引用来终止绑定。

编辑:在你之后

您正在倒退:那OnItemsSourcePropertyChanged不应该设置源,它实际上根本不应该存在。
您必须在 RecentSearchGrid.xaml 中绑定到在RecentSearchGrid.xaml.cs声明的ItemsSource依赖属性

于 2013-01-04T17:09:05.247 回答
0

我认为这里没有足够的信息来回答您的问题。以下简单的应用程序反映了我在问题中看到的场景,它按预期工作:

// MySettings.cs
public static class MySettings
{
    public static IList RecentSearchedRepo = new ObservableCollection<object>();
}

// MyVm.cs
public class MyVm : INotifyPropertyChanged
{
    private IList recentSearch = new ObservableCollection<object>();

    public event PropertyChangedEventHandler PropertyChanged;

    public MyVm()
    {
        this.RecentSearch = MySettings.RecentSearchedRepo;
    }

    public IList RecentSearch
    {
        get { return recentSearch; }
        set 
        { 
            recentSearch = value;
            this.RaisePropertyChanged("RecentSearch");
        }
    }

    private void RaisePropertyChanged(string p)
    {
        if (this.PropertyChanged != null) this.PropertyChanged(this, new PropertyChangedEventArgs(p));
    }
}

// MainWindow.xaml.cs
public partial class MainWindow : Window
{
    public MainWindow()
    {
        this.InitializeComponent();

        // Initialization as described in the question
        MySettings.RecentSearchedRepo.Add("SearchItem1");
        MySettings.RecentSearchedRepo.Add("SearchItem2");

        this.DataContext = new MyVm();
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        // Add a new item later
        MySettings.RecentSearchedRepo.Add("NewlyAddedSearchItem");
    }
}

// MainWindow.xaml
<Window x:Class="ScratchWpf.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <DockPanel>
        <Button DockPanel.Dock="Bottom" Content="Add new Search Item" Click="Button_Click_1" />
        <ListBox ItemsSource="{Binding RecentSearch}" />
    </DockPanel>
</Window>

我将尝试戴上我的通灵帽,并询问您是否在错误的收藏中添加了其他物品。添加单个项目后是否会重新创建集合并将其放置在绑定中,但后来的项目被添加到原始集合而不是新集合?

鉴于您说RecentSearchGrid 是一个UserControl,我们还可以推断ItemsSource 的实现可能是自定义的,而不是从ItemsControl 继承的标准实现。最近SearchGrid 可能以某种方式错误地破坏了绑定。

我同意狒狒的观点。目的是OnItemsSourcePropertyChanged什么?在典型的实现中,我不希望它存在。

于 2013-01-04T17:21:36.057 回答
0

问题可能如下: 如果实例没有更改,则不会调用OnItemsSourcePropertyChanged 。从 WPF 的角度来看,当您RaisePropertyChangeEvent,但绑定集合的实例没有改变时,PropertyChange处理程序根本不会被调用。

Settings.RecentSearchedRepo在应用程序的整个生命周期中是同一个实例吗?

于 2013-01-04T18:21:49.240 回答