C# 如何通过C设置LongListSelector.ItemSource#类似ItemsSource的代码=";{Binding CustomSounds.Items}";
我有一个这样的长列表选择器,现在我在CustomSounds.Items中更改了一些内容,并希望重新加载它。如何在代码隐藏文件CustomSoundLongListSelector.ItemSource=…中执行此操作您应该在代码隐藏文件中执行以下操作:C# 如何通过C设置LongListSelector.ItemSource#类似ItemsSource的代码=";{Binding CustomSounds.Items}";,c#,windows-phone-8,longlistselector,itemsource,C#,Windows Phone 8,Longlistselector,Itemsource,我有一个这样的长列表选择器,现在我在CustomSounds.Items中更改了一些内容,并希望重新加载它。如何在代码隐藏文件CustomSoundLongListSelector.ItemSource=…中执行此操作您应该在代码隐藏文件中执行以下操作: <phone:LongListSelector Margin="0,0,-12,0" ItemsSource="{Binding CustomSo
<phone:LongListSelector
Margin="0,0,-12,0"
ItemsSource="{Binding CustomSounds.Items}"
LayoutMode="Grid"
GridCellSize="150,150"
ItemTemplate="{StaticResource CustomSoundTileDataTemplate}"
SelectionChanged="LongListSelector_SelectionChanged"
x:Name="CustomSoundLongListSelector"
/>
注意,“模型”在这里是页面上的DataContext设置,或者更具体地说是在LongListSelector上设置的。您可能希望在ViewModel中实现
INotifyPropertyChanged
接口,以通知视图重新加载数据。我认为这是MVVM中非常标准的模式
基本上:
公共类视图模型:INotifyPropertyChanged{
CustomSoundLongListSelector.ItemsSource = model.CustomSounds.Items;
然后,当您更改项目时,您会说:
通知财产变更(“项目”)
如果您将对item source使用Observable collection,则不必实现
INotifyPropertyChanged
它将自行工作CustomSoundLongListSelector.ItemSource=CustomSounds.Items直接使用它..或使您的Items集合成为Observable collection。
//... your other VM stuff...
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
} }