Skip to main content
 首页 » 编程设计

c#之Windows Phone 7 : Making ListBox items change dynamically

2024年02月23日16wuhuacong

我正在创建一个 Windows Phone 应用程序,它将播放从列表中选择的一系列声音片段。我正在使用 MVVM(模型 View View 模型)设计模式,并为我的数据设计了一个模型,并为我的页面设计了一个 View 模型。 ListBox 的 XAML 如下所示:

<ListBox x:Name="MediaListBox" Margin="0,0,-12,0" ItemsSource="{Binding Media}" SelectionChanged="MediaListBox_SelectionChanged" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"> 
 
                <ListBox.ItemTemplate > 
                    <DataTemplate> 
                        <StackPanel Margin="0,0,0,17" Width="432" Orientation="Horizontal"> 
                            <Image Source="../Media/Images/play.png" /> 
                            <StackPanel > 
                                <TextBlock Text="{Binding Title}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/> 
                                <TextBlock Text="{Binding ShortDescription}" TextWrapping="Wrap" Margin="12,-6,12,0" Visibility="{Binding ShortDescriptionVisibility}" Style="{StaticResource PhoneTextSubtleStyle}"/> 
                                <TextBlock Text="{Binding LongDescription}" TextWrapping="Wrap" Visibility="{Binding LongDescriptionVisibility}" /> 
                                <StackPanel> 
                                    <Slider HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Visibility="{Binding LongDescriptionVisibility}" ValueChanged="Slider_ValueChanged" LargeChange="0.25" SmallChange="0.05" /> 
                                </StackPanel> 
                            </StackPanel> 
                        </StackPanel> 
                    </DataTemplate> 
                </ListBox.ItemTemplate> 
            </ListBox> 

我的问题是:我希望能够展开和折叠列表框中的部分项目。如您所见,我对可见性进行了绑定(bind)。该绑定(bind)来自 MediaModel。但是,当我在 ObservableCollection 中更改此属性时,页面不会更新以反射(reflect)这一点。

此页面的 ViewModel 如下所示:

public class ListenPageViewModel : INotifyPropertyChanged 
{ 
    public ListenPageViewModel() 
    { 
        this.Media = new ObservableCollection<MediaModel>; 
 
    } 
 
    /// <summary> 
    /// A collection for MediaModel objects. 
    /// </summary> 
    public ObservableCollection<MediaModel> Media { get; private set; } 
 
    public bool IsDataLoaded { get; private set; } 
 
    /// <summary> 
    /// Creates and adds the media to their respective collections. 
    /// </summary> 
    public void LoadData() 
    { 
        this.Media.Clear(); 
        this.Media.Add(new MediaModel() 
        { 
            Title = "Media 1", 
            ShortDescription = "Short here.", 
            LongDescription = "Long here.", 
            MediaSource = "/Media/test.mp3", 
            LongDescriptionVisibility = Visibility.Collapsed, 
            ShortDescriptionVisibility = Visibility.Visible 
        }); 
 
        this.Media.Add(new MediaModel() 
        { 
            Title = "Media 2", 
            ShortDescription = "Short here.", 
            LongDescription = "Long here.", 
            MediaSource = "/Media/test2.mp3", 
            LongDescriptionVisibility = Visibility.Collapsed, 
            ShortDescriptionVisibility = Visibility.Visible 
        }); 
 
        this.IsDataLoaded = true; 
    } 
 
    public event PropertyChangedEventHandler PropertyChanged; 
    private void NotifyPropertyChanged(String propertyName) 
    { 
        PropertyChangedEventHandler handler = PropertyChanged; 
        if (null != handler) 
        { 
            handler(this, new PropertyChangedEventArgs(propertyName)); 
        } 
    } 
} 

绑定(bind)工作正常,我看到显示的数据;但是,当我更改属性时,列表不会更新。我相信这可能是因为当我更改可观察集合中的内容时,属性更改事件没有触发。

我该怎么做才能解决这个问题?我已经四处寻找这方面的一些信息,但是很多教程都没有涵盖这种行为。任何帮助将不胜感激!

谢谢

编辑:根据要求,我添加了 MediaModel 代码:

    public class MediaModel : INotifyPropertyChanged 
{ 
    public string Title { get; set; } 
    public string ShortDescription { get; set; } 
    public string LongDescription { get; set; } 
    public string MediaSource { get; set; } 
    public Visibility LongDescriptionVisibility { get; set; } 
    public Visibility ShortDescriptionVisibility { get; set; } 
 
    public MediaModel() 
    { 
    } 
 
    public MediaModel(string Title, string ShortDescription, string LongDescription, string MediaSource, Visibility LongDescriptionVisibility, Visibility ShortDescriptionVisibility) 
    { 
        this.Title = Title; 
        this.ShortDescription = ShortDescription; 
        this.LongDescription = LongDescription; 
        this.MediaSource = MediaSource; 
        this.LongDescriptionVisibility = LongDescriptionVisibility; 
        this.ShortDescriptionVisibility = ShortDescriptionVisibility; 
    } 
    public event PropertyChangedEventHandler PropertyChanged; 
    private void NotifyPropertyChanged(String propertyName) 
    { 
        PropertyChangedEventHandler handler = PropertyChanged; 
        if (null != handler) 
        { 
            handler(this, new PropertyChangedEventArgs(propertyName)); 
        } 
    } 
} 

最初,我没有让此类实现 INotifyPropertyChanged。我这样做是为了看看它是否能解决问题。我希望这可能只是一个数据对象。

请您参考如下方法:

尝试将您的详细描述可见性属性更改为此以查看是否可以解决问题

private Visibility _longDescriptionVisibility; 
public Visibility LongDescriptionVisibility 
{ 
    get { return _longDescriptionVisibility; } 
    set 
    { 
        _longDescriptionVisibility = value; 
        NotifyPropertyChanged("LongDescriptionVisibility"); 
    } 
}        

如果它确实对简短描述属性进行了相同的更改。