如何动态更改windows phone 8中按钮的可见性

本文关键字:phone 按钮 可见性 windows 何动态 动态 | 更新日期: 2023-09-27 18:30:10

我有一个ListBox,它有两个文本块和一个按钮。默认情况下,按钮的可见性是折叠的。

列表框

 <ListBox x:Name="companiesList" Height="{Binding ActualHeight, ElementName=ContentPanel}"  SelectionChanged="companiesList_SelectionChanged" ScrollViewer.VerticalScrollBarVisibility="Auto">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid x:Name="listItem" Background="{Binding BackgroundBrush}">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="50"/>
                            <RowDefinition Height="35"/>
                            <RowDefinition Height="40"/>
                            <RowDefinition Height="10"/>
                        </Grid.RowDefinitions>
                        <StackPanel x:Name="namePanel" Grid.Row="0" Orientation="Horizontal" Margin="5,0,0,0" Height="50">
                            <TextBlock x:Name="nameTextBlock" Text="{Binding CompanyName}" Foreground="#FF501F6E" Style="{StaticResource PhoneTextNormalStyle}" HorizontalAlignment="Left" FontSize="28" MaxHeight="50" TextTrimming="WordEllipsis" Margin="0,0,0,0" Width="460" FontWeight="Bold" FontFamily="Fonts/Riwaj.ttf#Riwaj"/>
                        </StackPanel>
                        <StackPanel x:Name="addressPanel" Grid.Row="1" Orientation="Horizontal" Margin="5,-5,0,5" Height="35">
                            <TextBlock x:Name="addressTextBlock"  Text="{Binding Address}" Foreground="#FF1F1F1F" Style="{StaticResource PhoneTextNormalStyle}" HorizontalAlignment="Left" FontSize="20" MaxHeight="35" TextTrimming="WordEllipsis" Margin="0,0,0,0" Width="460" FontFamily="Fonts/Riwaj.ttf#Riwaj"/>
                        </StackPanel>
                        <StackPanel x:Name="phonePanel" Grid.Row="2" Orientation="Horizontal" Margin="5,0,0,0" Height="35">
                            <Button x:Name="sponseredButton" Style ="{StaticResource ButtonStyleIB}"  HorizontalAlignment="Right" Click="spon_button_clicked" Width="Auto" Height="Auto" Visibility="Collapsed">
                                <Image x:Name="sponseredButtonImage" Source="{Binding SponseredImageSource}" Stretch="None" />
                            </Button>
                        </StackPanel>
                        <Image  x:Name="line" Grid.Row="3" Width="460" HorizontalAlignment="Center" Source="Images/separator.png"  />
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

现在在我的代码隐藏文件(.cs文件)中,我想根据某些条件更改按钮的可见性

请帮我怎么做。

如何动态更改windows phone 8中按钮的可见性

您只需在XAML中将按钮的Visibility属性设置为Collapsed,然后当您在CS代码中的特定条件满足时,将按钮的Visibility设置为`Visible。

在XAML后面的.cs代码中添加以下代码段:

sponseredButton.Visibility = Visibility.Visible;

您已经使用visul树查找列表框项

如何访问WP7 中ListBox ItemTemplate中的控件

如果有任何查询,请告诉我

实际需要实现的是一个转换器。假设您希望按钮仅对特定赞助商可见:

<StackPanel x:Name="phonePanel" Grid.Row="2" Orientation="Horizontal" Margin="5,0,0,0" Height="35">
    <Button x:Name="sponseredButton" Style ="{StaticResource ButtonStyleIB}"  HorizontalAlignment="Right" Click="spon_button_clicked" Width="Auto" Height="Auto" Visibility="{Binding SponseredName Converter={StaticResource SponsorConverter}}">
        <Image x:Name="sponseredButtonImage" Source="{Binding SponseredImageSource}" Stretch="None" />
    </Button>
</StackPanel>

和转换器如下:

public class SponsorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        // Assuming that you have the sponsor's name as string as follows: 
        var sn = SponseredName.ToString();
        if (sn.Equals("A"))
           return Visibility.Visible;
        elseif () ...
        return Visibility.Collapsed;
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return Visibility.Collapsed;
    }
}

不要忘记在将要使用的XAML文件中定义转换器。

在此处查找有关转换器的更多信息。