简体   繁体   English

将新项添加到ItemsSource时更新ItemsControl

[英]Updating ItemsControl when new Item is added to the ItemsSource

Okay this seems like a pretty easy thing to do, but I've been looking for a solution for over an hour and couldn't come up with anything. 好吧,这似乎是一件非常容易的事情,但我一直在寻找一个小时以上的解决方案,并且无法想出任何东西。 In my WP8 application, I have an ItemsControl which I bind data procedurally. 在我的WP8应用程序中,我有一个ItemsControl ,我在程序上绑定数据。 The bound data is a static ObservableCollection . 绑定数据是静态ObservableCollection When I add new items to this collection via another page, I expect to see the new item on my ItemsControl . 当我通过另一个页面向此集合添加新项目时,我希望在我的ItemsControl上看到新项目。 After adding a new item to the collection, the ItemsControl still looks empty, even though its ItemsSource seems to contain items. 将新项添加到集合后, ItemsControl仍然显示为空,即使其ItemsSource似乎包含项。

Edit: The code I wrote looks like below 编辑:我写的代码如下所示

The ItemsControl is defined on the page as follows: ItemsControl在页面上定义如下:

<ItemsControl Name="MyItemsControl" 
                              Grid.Row="4"
                              Grid.ColumnSpan="2">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Grid>
                                <TextBlock Text="{Binding someData}" 
                                           Margin="24"
                                           Foreground=""/>
                            </Grid>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>

Binding is handled as follows: 绑定处理如下:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
     MyItemsControl.ItemsSource = SomeClass.myObservableCollection;
}

Class where the static collection resides: 静态集合所在的类:

class SomeClass 
{
    public static ObservableCollection<MyData> myObservableCollection { set; get;}
}

MyData: 迈德特:

class MyData
{
    public string someData { set; get; }
}

I think the problem is that your ObservableCollection is null, if you want to use property it can look like this: 我认为问题是你的ObservableCollection为null,如果你想使用属性,它可能如下所示:

class SomeClass
{
    private static ObservableCollection<MyData> myObservableCollection = new ObservableCollection<MyData>();

    public static ObservableCollection<MyData> MyObservableCollection
    {
         get { return SomeClass.myObservableCollection; }
         set { SomeClass.myObservableCollection = value; }
    }         
}

Then: 然后:

MyItemsControl.ItemsSource = SomeClass.MyObservableCollection;
SomeClass.MyObservableCollection.Add(new MyData() { someData = "Romasz" });

And of course it can be even easier (if you don't need property): 当然,它可以更容易(如果你不需要财产):

public static ObservableCollection<MyData> myObservableCollection = new ObservableCollection<MyData>();

And you can move MyItemsControl.ItemsSource = SomeClass.MyObservableCollection; 你可以移动MyItemsControl.ItemsSource = SomeClass.MyObservableCollection; to the constructior of your Page - there is no need to do it every time you Navigate to the Page. 对于您的页面的构造 - 每次导航到页面时都不需要这样做。

And remove Foreground="" from your XAML code. 并从您的XAML代码中删除Foreground=""

EDIT - after comment 编辑 - 评论后
It's hard to say for me where can be a problem as I don't see your whole code, but please consider this example which works fine for me: 很难说我哪里可以成为一个问题,因为我没有看到你的整个代码,但请考虑这个适合我的例子:
In XAML: 在XAML中:

<ItemsControl Name="MyItemsControl" Grid.Row="3">
      <ItemsControl.ItemTemplate>
            <DataTemplate>
               <Grid>
                  <TextBlock Text="{Binding someData}" Margin="24"/>
               </Grid>
            </DataTemplate>
       </ItemsControl.ItemTemplate>
</ItemsControl>

In code behind: 代码背后:

class SomeClass
{
   public static ObservableCollection<MyData> myObservableCollection = new ObservableCollection<MyData>();
}

class MyData
{
   public string someData { set; get; }
}

public MainPage()
{
   InitializeComponent();

   MyItemsControl.ItemsSource = SomeClass.myObservableCollection;
   SomeClass.myObservableCollection.Add(new MyData() { someData = "First" });
   SomeClass.myObservableCollection.Add(new MyData() { someData = "Second" });
}

Here is example of updating data 以下是更新数据的示例

private void UpdateData()
        {
            try
            {
                MainWindow.cmdSel = new SqlCommand("SELECT Name FROM Cities order by ID asc", MainWindow.conn);
                DataSet dtst = new DataSet();
                SqlDataAdapter adpt = new SqlDataAdapter(); 
                try
                {

                    adpt.SelectCommand = MainWindow.cmdSel;
                    adpt.Fill(dtst, "Cities");
                    lstCity.DataContext = dtst;

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }


            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message);

            }
        }

After you add some items in your ListItem 在ListItem中添加一些项目后

    private void AddCity(object sender, MouseButtonEventArgs e)
    {
        if (TxtCity.Text != string.Empty)
        {
            Add(TxtCity.Text);
            UpdateData();           ////Here I am updating my listitem
        }
        else
        {
            MessageBox.Show("Add city");
        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM