0

以下のコードは次のように機能します: 並べ替えられた名前のリストを含むフォームが表示されます。ボタンがクリックされると、新しい名前がリストのテキストボックスから適切にソートされた位置に追加されます。リストの項目をダブルクリックすると、「AAA」というプレフィックスが付けられ、リストの一番上に配置されます。ObservableCollection を List に変更すると、この動作はもちろん消えます。しかし、どうすれば真似できますか?INotifyPropertyChanged を実装しようとしましたが、リストボックスの BindingExpression で UpdateTarget() メソッドを呼び出しましたが、すべて役に立ちませんでした...

次の XAML があります。

<Window x:Class="CollectionViewSpike.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<StackPanel>
    <TextBox Name="NewNameBox" Text="{Binding NewName, UpdateSourceTrigger=PropertyChanged}"/>
    <Button Click="Button_Click"">Add</Button>
    <ListBox Name="listbox" ItemsSource="{Binding MyCollectionViewSource.View}"
             MouseDoubleClick="listbox_MouseDoubleClick"/>
</StackPanel>

背後にあるコード:

using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;

namespace CollectionViewSpike
{
   public partial class Window1: Window
   {
    public ObservableCollection<string> Names { get; set; }

    public static DependencyProperty NewNameProperty = DependencyProperty.Register("NewName", typeof (string),
                                                                           typeof (Window1),
                                                                           new PropertyMetadata(string.Empty));

    public string NewName
    {
        get { return (string) GetValue(NewNameProperty); }

        set{ SetValue(NewNameProperty,value);}
    }

    public Window1()
    {
        InitializeComponent();
        var names = new string[] { "onno", "friso","andre"};
        Names = new ObservableCollection<string>(names);
        collectionViewSource = new CollectionViewSource { Source = Names };
        collectionViewSource.SortDescriptions.Add(
                     new SortDescription("", ListSortDirection.Ascending));
        DataContext = this;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        NewName = NewName ?? "???";
        Names.Add(NewName);
        NewName = string.Empty;
    }

    private CollectionViewSource collectionViewSource;
    public CollectionViewSource MyCollectionViewSource
    {
        get { return collectionViewSource; }
        private set { collectionViewSource = value;}
    }

    private void listbox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        var user = (string)listbox.SelectedItem;
        int index = Names.IndexOf(user);
        Names[index]="AAA"+Names[index];
    }
}

}

4

2 に答える 2

0

ObservableCollection を使用しない理由がわかりません-それがその目的です。ただし、いずれにせよ、INotifyCollectionChanged を実装して、コンテンツまたは順序が変更されたことをバインディングに知らせる必要があります。

ObservableCollection の msdn リファレンス

于 2009-11-03T14:00:30.560 に答える
0

それを見つけた。MyCollectionViewSource.View.Refresh() をメソッドに追加して、基になる観察不可能な列挙型を変更します<T> Observable <string> Names が List <string> Names になったと仮定します。

   private void Button_Click(object sender, RoutedEventArgs e)
    {
        NewName = NewName ?? "???";
        Names.Add(NewName);
        NewName = string.Empty;
        MyCollectionViewSource.View.Refresh();
    }

   private void listbox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        var user = (string)listbox.SelectedItem;
        int index = Names.IndexOf(user);
        Names[index]="AAA"+Names[index];
        MyCollectionViewSource.View.Refresh();
    } 
于 2009-11-03T17:24:41.920 に答える