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

时间:2016-01-12 21:09:33

标签: c# events windows-phone-8 windows-phone-8.1 longlistselector

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

  

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

我将它们设置为collapsed,我想在selection_changed的{​​{1}}事件中启用它们。

XAML:

LongListSelector

选择更改了活动:

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

1 个答案:

答案 0 :(得分:0)

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

视图模型:

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