1

ItemsControl への項目の追加に問題があります。これは私の XAML ページです:

<ScrollViewer  Grid.Row="4">
    <ItemsControl Name="items">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                    <StackPanel Name="ContentControl">
                        <Canvas Name="canvas1" Height="60" VerticalAlignment="Top">
                            <TextBlock Text="{Binding RecordedTime}" Canvas.Left="10" Canvas.Top="7" Width="370"  FontSize="36"/>
                            <Controls:RoundButton Name="save" Canvas.Left="380" Height="58" Canvas.Top="6" />
                        </Canvas>
                    </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</ScrollViewer>

私のコードビハインドには、その中にイベントがあります。

records.Add(new item { Item = date.Now.ToString() });

        items.ItemsSource = records; 

すべての変数はすでに定義されています。

問題は、イベントが何度もトリガーされたときに、初回のみ ItemsControl に追加され、他は表示されないことです。それで、誰が問題がどこにあるか知っていますか?

4

1 に答える 1

2

recordsとして宣言する必要がありObservableCollectionます。ItemsSourceリストボックスのプロパティに一度だけ割り当ててから、コレクションのみを使用してください。InitializeComponentsたとえば、メソッドを呼び出した後、ページのコンストラクターでそれを行うことができます。

public ObservableCollection<item> Records { get; set; }

// Constructor
public Page3()
{
    InitializeComponent();

    this.Records = new ObservableCollection<item>();

    this.items.ItemsSource = this.Records;
}

public void AddItem()
{
    // Thanks to the ObservableCollection,
    // the listbox is notified that you're adding a new item to the source collection,
    // and will automatically refresh its contents
    this.Records.Add(new item { Item = DateTime.Now.ToString() });
}
于 2013-07-02T06:00:22.280 に答える