0

我有两个文本块和一个按钮的 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 文件)后面的代码中,我想根据某些条件更改按钮的可见性。

请帮助我如何做到这一点。

4

3 回答 3

1

您可以简单地通过Visibility将按钮的属性设置为 来执行此操作CollapsedXAML并且当您在 CS 代码中的特定条件满足时,将按钮的属性设置Visibility为“可见”。

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

sponseredButton.Visibility = Visibility.Visible;
于 2015-06-15T01:06:29.570 回答
0

你真正需要的是一个转换器。假设您希望该按钮仅对特定赞助商可见:

<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 文件中定义您的转换器。

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

于 2013-09-06T05:33:52.453 回答
0

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

如何访问放置在 WP7 中的 ListBox ItemTemplate 中的控件

如果有任何疑问,请告诉我

于 2013-09-05T07:37:07.763 回答