繁体   English   中英

将新项添加到ItemsSource时更新ItemsControl

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

好吧,这似乎是一件非常容易的事情,但我一直在寻找一个小时以上的解决方案,并且无法想出任何东西。 在我的WP8应用程序中,我有一个ItemsControl ,我在程序上绑定数据。 绑定数据是静态ObservableCollection 当我通过另一个页面向此集合添加新项目时,我希望在我的ItemsControl上看到新项目。 将新项添加到集合后, ItemsControl仍然显示为空,即使其ItemsSource似乎包含项。

编辑:我写的代码如下所示

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>

绑定处理如下:

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

静态集合所在的类:

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

迈德特:

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

我认为问题是你的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; }
    }         
}

然后:

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

当然,它可以更容易(如果你不需要财产):

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

你可以移动MyItemsControl.ItemsSource = SomeClass.MyObservableCollection; 对于您的页面的构造 - 每次导航到页面时都不需要这样做。

并从您的XAML代码中删除Foreground=""

编辑 - 评论后
很难说我哪里可以成为一个问题,因为我没有看到你的整个代码,但请考虑这个适合我的例子:
在XAML中:

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

代码背后:

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" });
}

以下是更新数据的示例

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);

            }
        }

在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