LongListSelector在选择更改时设置图像可见性

本文关键字:设置 图像 可见性 选择 LongListSelector | 更新日期: 2023-09-27 18:30:12

我有一个LongListSelector,里面有一些textblocks和图像。

如何以编程方式设置图像的可见性?

我将它们设置为collapsed,并希望在LongListSelectorselection_changed事件上启用它们。

XAML:

<phone:LongListSelector Name="LongListSel" Margin="0,-38,-22,2" ItemsSource="{Binding Items}" SelectionChanged="LongListSelector_SelectionChanged">
                <phone:LongListSelector.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" Margin="12,2,0,4" Height="110" Width="432">
                            <StackPanel Width="311" Margin="0,0,0,0">
                                <TextBlock Text="{Binding LineOne}" TextWrapping="Wrap" Margin="10,0" Style="{StaticResource PhoneTextExtraLargeStyle}" FontSize="{StaticResource PhoneFontSizeLarge}" />
                                <TextBlock Text="{Binding LineTwo}" TextWrapping="Wrap" Margin="10,-2,10,0" Style="{StaticResource PhoneTextSubtleStyle}" />
                            </StackPanel>
                            <StackPanel Name="playImage" Height="50" Width="50" Margin="0,0,10,0">
                                <Image Source="Assets/Tiles/Iconsmind-Outline-Play-Music.ico" Visibility="{Binding ImageVis}" Width="50" Height="50" HorizontalAlignment="Left" Tap="Image_Tap_1"/>
                            </StackPanel>
                            <StackPanel Name="downloadImage" Height="50" Width="50" Margin="0,0,0,0">
                                <Image Source="Assets/Tiles/Download.ico" Visibility="{Binding ImageVis}" Width="50" Height="50" HorizontalAlignment="Left" Tap="Image_Tap_1"/>
                            </StackPanel>
                        </StackPanel>
                    </DataTemplate>
                </phone:LongListSelector.ItemTemplate>
            </phone:LongListSelector>
        </phone:PanoramaItem>

选择更改事件:

private void LongListSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ItemViewModel item = new ItemViewModel();
            item.ImageVis = Visibility.Visible;
            //it can't be called the way you are doing it of course and it still doesn't work
        }

ViewModel

        private Visibility _imageVis;
        public Visibility ImageVis
        {
            get { return _imageVis; }
            set
            {
                _imageVis = value;
                NotifyPropertyChanged("ImageVis");
            }
        }

LongListSelector在选择更改时设置图像可见性

创建Visibility类型的属性并将其绑定到图像可见性。

ViewModel:

    private Visibility _DownloadImage;
    public Visibility DownloadImage
    {
        get { return _DownloadImage; }
        set
        {
            if (_DownloadImage != value)
            {
                _DownloadImage = DownloadImage;
                OnPropertyChanged("DownloadImage");
            }
        }
    }

xaml

<Image Source="Assets/Tiles/Download.ico" Visibility="{Binding DownloadImage}" ... />

xaml.cs(我更愿意将命令也绑定到ViewModel,以保持MVVM模式)

private void LongListSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        DownloadImage = Visibility.Visible;
    }