如何在Windows Phone的“项目列表”控件中显示和隐藏图像

时间:2013-03-05 19:13:52

标签: silverlight windows-phone-7

我有一个列表控件,每个项目包含两个图像和文本。单击我要隐藏的每个项目或在所选列表项目上显示所选图像。

以下是XAML代码段:

            <ListBox x:Name="list" SelectionChanged="list_SelectionChanged"  >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding Name}"/>
                        <Image Source="{Binding ImagePath}" Stretch="None"/>
                        <Image Source="{Binding ImagePath}" Stretch="None"  
                               Visibility="{Binding ImageVisibility, 
                            Converter={StaticResource boolVisibilityConverter}}" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

C#代码:

 dataSource = new ObservableCollection<ImageData>() 
        { 
        new ImageData(){Name = "User1:", ImagePath="/Images/user1.png", ImageVisibility = false},
        new ImageData(){Name = "User1:", ImagePath="/Images/user1.png", ImageVisibility = true},
        new ImageData(){Name = "User1:", ImagePath="/Images/user1.png", ImageVisibility = true},
        new ImageData(){Name = "User2:", ImagePath="/Images/user2.png", ImageVisibility = true}
        };

列表选择更改事件:

        private void list_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        ((ImageData)(((object[])(e.AddedItems))[0])).ImageVisibility = false;
        list.UpdateLayout();
    }

ImageData类:

 public class ImageData
    {
        public string ImagePath { get; set; }
        public string Name { get; set; }
        public bool ImageVisibility { get; set; }
    }

图像可见性转换器:

public class BooleanToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        bool flag = false;
        if (value is bool)
        {
            flag = (bool)value;
        }
        else if (value is bool?)
        {
            bool? nullable = (bool?)value;
            flag = nullable.HasValue ? nullable.Value : false;
        }
        return (flag ? Visibility.Visible : Visibility.Collapsed);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return ((value is Visibility) && (((Visibility)value) == Visibility.Visible));
    }

}

请帮助我完成此类功能。

1 个答案:

答案 0 :(得分:0)

您需要使用INotifyPropertyChanged接口http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx

例如:

public class ImageData : INotifyPropertyChanged
{
    private bool _imageVisibility;
    public string ImagePath { get; set; }
    public string Name { get; set; }
    public bool ImageVisibility
    {
        get
        {
            return _imageVisibility;
        }
        set
        {
            _imageVisibility = value;
            OnPropertyChanged("ImageVisibility");
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

根据您的任务进行完整更改,您可以看到here (dropbox)