【问题标题】:How to set LongListSelector.ItemSource by C# code like ItemsSource="{Binding CustomSounds.Items}"如何通过 C# 代码设置 LongListSelector.ItemSource,例如 ItemsSource="{Binding CustomSounds.Items}"
【发布时间】:2013-11-14 17:42:30
【问题描述】:
            <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"
                />

我有一个这样的 LongListSelector,现在我在 CustomSounds.Items 中更改了一些内容并想重新加载它。我如何在文件后面的代码中做到这一点,CustomSoundLongListSelector.ItemSource = ...

【问题讨论】:

  • CustomSoundLongListSelector.ItemSource = CustomSounds.Items 直接使用它..或将您的项目集合设为 Observable Collections。

标签: c# windows-phone-8 longlistselector itemsource


【解决方案1】:

您应该在代码隐藏中这样做:

CustomSoundLongListSelector.ItemsSource = model.CustomSounds.Items;

注意这里的“模型”是在您的页面上设置的 DataContext,或者更具体地说是在 LongListSelector 上设置的。

【讨论】:

    【解决方案2】:

    您可能希望在 ViewModel 中实现 INotifyPropertyChanged 接口,以通知视图重新加载您的数据。我认为这是 MVVM 中非常标准的模式。

    基本上:

    公共类 ViewModel : INotifyPropertyChanged {

    //... your other VM stuff...
    
    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (null != handler)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    } }
    

    然后当您更改项目时,您会说:

    NotifyPropertyChanged("Items");

    【讨论】:

      【解决方案3】:

      如果您将 Observable 集合用于项目源您不必实现 INotifyPropertyChanged 它会自行工作

      【讨论】:

        猜你喜欢
        • 2012-03-30
        • 1970-01-01
        • 2016-10-02
        • 2018-07-03
        • 2011-06-05
        • 2011-08-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多