Reputation: 899
I have ListBox with two text blocks and one button. By default the button's visibilty is collapsed.
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>
Now in my code behind file(.cs file) I want to change the visibility of button based on the some condition.
Please help me how to do that.
Upvotes: 0
Views: 3110
Reputation: 8522
You can simply do this with setting the Visibility
property of the button to Collapsed
in the XAML
and when your specific condition in the CS code meets set the Visibility
of the button to `Visible.
In the .cs code behind the XAML add this snippet:
sponseredButton.Visibility = Visibility.Visible;
Upvotes: 1
Reputation: 1386
What you actually need to achieve that is a Converter. Lets assume that you want the button to be visible only for specific sponsors:
<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>
And the converter as follows:
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;
}
}
Don't forget to define you converter in the XAML file that you will be using it.
Find more about Converters here.
Upvotes: 0
Reputation: 1435
you have use visul tree for findind list box item
How to access a Control placed inside ListBox ItemTemplate in WP7
if any query than let me know
Upvotes: 0