Windows Phone 7:使ListBox项动态更改 [英] Windows Phone 7: Making ListBox items change dynamically

查看:90
本文介绍了Windows Phone 7:使ListBox项动态更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个Windows Phone应用,该应用将播放从列表中选择的一系列声音片段.我正在使用MVVM(模型视图视图模型)设计模式,并为我的数据设计了一个模型,并为我的页面设计了一个视图模型.这是ListBox的XAML外观:

I am working on creating a Windows Phone app that will play a series of sound clips selected from a list. I am using the MVVM (Model View View-Model) Design pattern and have designed a model for my data, along with a view model for my page. Here is what the XAML for the ListBox looks like:

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

我的问题是:我希望能够扩展和折叠ListBox中的部分项目.如您所见,我对可见性具有约束力.该绑定来自MediaModel.但是,当我在ObservableCollection中更改此属性时,页面不会更新以反映此情况.

My question is this: I want to be able to expand and collapse part of the items in the ListBox. As you can see, I have a binding for the visibility. That binding is coming from the MediaModel. However, when I change this property in the ObservableCollection, the page is not updated to reflect this.

此页面的ViewModel如下:

The ViewModel for this page looks like this:

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

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

The bindings work correctly and I am seeing the data displayed; however, when I change the properties, the list does not update. I believe that this may be because when I change things inside the observable collection, the property changed event is not firing.

该如何解决?我四处寻找有关此问题的信息,但是许多教程并未涵盖这种行为.任何帮助将不胜感激!

What can I do to remedy this? I have poked around for some info on this, but many of the tutorials don't cover this kind of behavior. Any help would be greatly appreciated!

谢谢

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

As requested, I have added the MediaModel code:

    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.我这样做是为了看看是否可以解决问题.我希望这可能只是一个数据对象.

Originally, I did not have this class implement the INotifyPropertyChanged. I did this to see if it would solve the problem. I was hoping this could just be a data object.

推荐答案

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

Try changing your long description visibility property to this to see if that fixes it

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

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

If it does make the same change to the short description property.

这篇关于Windows Phone 7:使ListBox项动态更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆