[英]UWP update a itemscontrol with an array that keeps updating
I have an array that keeps changing its values, because of this I want to have the apps UI refreshing every time the array's values do. 我有一个数组,它会不断更改其值,因此,我想让应用程序UI每次数组的值刷新一次。 I have this bound with an itemsControl. 我有一个itemsControl绑定。 I can show the first array's values but then I can't update them I have tried .items.Clear() but its not working. 我可以显示第一个数组的值,但是之后我无法更新它们,因为我尝试过.items.Clear(),但无法正常工作。 Here are snippets of the .xaml and the xaml.cs. 这是.xaml和xaml.cs的片段。 I actually took the code of the .xaml from a question from this site. 实际上,我从该站点的问题中获取了.xaml的代码。
.xaml .xaml
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBox Text="Testing" IsReadOnly="True"></TextBox>
<ItemsControl x:Name="itemsControl"
ItemsSource="{Binding itemsControl}"
FontSize="24">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Width="Auto"
Margin="0 12"
HorizontalAlignment="Center">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<StackPanel Grid.Column="0"
Grid.Row="0"
Orientation="Horizontal">
<TextBlock Name="txtblk0" Text="{Binding}" />
</StackPanel>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
.xaml.cs .xaml.cs
String c = (new String(cArray));
string[] arr = null;
string[] data = null;
if (c != null)
{
arr = c.Split('\n');
if (arr.Length > 0)
{
data = arr[0].Split(',');
}
}
for(int index = 0; index < 4; index++)
{
itemsControl.Items.Add(float.Parse(data[index]));
}
itemsControl.Clear();
If anyone has an idea of how I can do this I will be very grateful, thanks in advance and I will try to answer any questions as soon as possible! 如果有人知道我该怎么做,我将不胜感激,在此先感谢您,我将尽快回答任何问题!
What you're missing is an understanding of how bindings are triggered to update. 您缺少的是对如何触发绑定进行更新的理解。
The INotifyPropertyChanged
interface contains a method ( PropertyChanged
) and when called and passed the name of a property will tell the binding system that the property has changed and the binding should be updated. INotifyPropertyChanged
接口包含一个方法( PropertyChanged
),并且在调用并传递属性名称时,该名称将告诉绑定系统该属性已更改,并且绑定应该更新。
INotifyCollectionChanged
is the equivalent for collections, and communicates when a collection has changed. INotifyCollectionChanged
与集合等效,并在集合更改时进行通信。 ie something added, removed, or the list cleared. 即添加,删除或清除列表的内容。
ObservableCollection<T>
contains an implementation of INotifyCollectionChanged
that makes it easy to work with lists, collections, etc. that change. ObservableCollection<T>
包含INotifyCollectionChanged
的实现,可轻松处理更改的列表,集合等。
If you used an ObservableCollection<float>
instead of an array you'd be able to modify the list and have the UI updated to reflect this easily. 如果使用ObservableCollection<float>
而不是数组,则可以修改列表并更新UI来轻松反映这一点。
As a starter, see the following which demonstrates how easy it is to use an ObservableCollection. 首先,请参阅以下内容,该示例演示了使用ObservableCollection多么容易。
XAML: XAML:
<StackPanel>
<Button Click="Button_Click">add an item</Button>
<ItemsControl ItemsSource="{Binding Items}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
code behind; 后面的代码;
public MainPage()
{
this.InitializeComponent();
// Initialize the property
this.Items = new ObservableCollection<string>();
// Use self as datacontext (but would normally use a separate viewmodel)
this.DataContext = this;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
// add a new item to the UI
this.Items.Add(DateTime.Now.ToString());
}
// The "collection" that is shown in the UI
public ObservableCollection<string> Items { get; set; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.