5

私のWPFアプリケーションでは、新しいアイテムをObservableCollectionviaに追加しButton Click Event Handlerます。今、私はこの追加されたアイテムをすぐに表示したいと思って ObservableCollectionBindingますItemsControl. 誰でも私の問題を解決できますか。コードは次のとおりです。

.XAML ファイル

    <dxlc:ScrollBox VerticalAlignment="Top">
        <ItemsControl x:Name="lstItemsClassM" ItemsSource="{Binding Path=topp,   Mode=TwoWay}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Vertical">
                        <Button Content="{Binding Name}"  Tag="{Binding PKId}"/>
                      </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

    </dxlc:ScrollBox>

.CS ファイル

     public ObservableCollection<ClassMM> topp { get; set; }

    int dv , startindex, lastindex;

    public MainWindow()
    {

        InitializeComponent();
        topp = new ObservableCollection<ClassMM>();
        startindex=dv=1;
        topp.Add(new ClassMM() {  PKId=dv, Name = "Test 1" });
        dv=2;
        topp.Add(new ClassMM() { PKId = dv, Name = "Test 2" });
        dv = 3;
        topp.Add(new ClassMM() { PKId = dv, Name = "Test 3" });

        dv = 4;
        topp.Add(new ClassMM() { PKId = dv, Name = "Test 4" });

        lastindex=dv = 5;
        topp.Add(new ClassMM() { PKId = dv, Name = "Test 5" });


    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        lastindex = dv = dv++;

        topp.Add(new ClassMM() { PKId = dv, Name =  musavebutton.Content.ToString() });
        foreach (var jk in topp.ToList())
        {
            MessageBox.Show(jk.Name);
        }
    }
     public class ClassMM : INotifyPropertyChanged
{
    public string _name;
    public int _pkid;


    public int PKId
    {
        get { return _pkid; }
        set
        {
            if (value != _pkid)
            {
                _pkid = value;
                NotifyPropertyChanged();
            }
        }
    }



    public string Name
    {
        get { return _name; }
        set
        {
            if (value != _name)
            {
                _name = value;
                NotifyPropertyChanged();
            }
        }
    }



    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged(String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
     }
   }

}

4

2 に答える 2

3

これは正しくありません: ItemsSource="{Binding topp, Mode=TwoWay}"。この場合、リストの内容ではなくTwoWay、バインドされたプロパティ自体の取得と設定に関係します。箱から出してアイテムの追加/削除通知を処理します。この場合、項目コントロールが の値を混乱させたくないので、正しいバインディングは.toppObservableListtopp{Binding topp}

于 2013-10-06T18:59:38.863 に答える