If you're using the MVVM pattern, you can add an additional IsSelected
property to your items that you're binding to the ListBox
:
public bool IsSelected
{
get { return _isSelected; }
set
{
_isSelected = value;
RaisePropertyChanged();
}
}
On the page view model add a SelectedItem
property which will set IsSelected
to all the items correctly:
public Item SelectedItem
{
get { return _selectedItem; }
set
{
if (_selectedItem != value)
{
if (_selectedItem != null)
{
_selectedItem.IsSelected = false;
}
if (value != null)
{
value.IsSelected = true;
}
_selectedItem = value;
RaisePropertyChanged();
}
}
}
Now you need to bind these two properties:
<ListBox ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" />
<Button Content="Click Me" Visibility="{Binding IsSelected, Converter={StaticResource VisibilityConverter}}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
You also need a converter for converting the bool
value to Visibility
:
public class VisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
if (value is bool)
{
return (bool) value ? Visibility.Visible : Visibility.Collapsed;
}
else
{
return Visibility.Collapsed;
}
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new System.NotImplementedException();
}
}
Declare it as a static resource on the page:
<Page.Resources>
<local:VisibilityConverter x:Key="VisibilityConverter" />
</Page.Resources>
Without the MVVM pattern you're best off using the Cimbalino multibinding behavior. First reference the Cimbalino Toolkit. Then you can configure the multibinding in XAML:
<ListBox ItemsSource="{Binding Items}" x:Name="MyListBox">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" />
<Button Content="Click Me">
<interactivity:Interaction.Behaviors>
<behaviors:MultiBindingBehavior PropertyName="Visibility" Converter="{StaticResource VisibilityMultiConverter}">
<behaviors:MultiBindingItem Value="{Binding}"/>
<behaviors:MultiBindingItem Value="{Binding SelectedItem, ElementName=MyListBox}"/>
</behaviors:MultiBindingBehavior>
</interactivity:Interaction.Behaviors>
</Button>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
Again you need a converter that checks if the item is currently selected:
public class VisibilityMultiConverter : MultiValueConverterBase
{
public override object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
return values[0] != null && values[0] == values[1] ? Visibility.Visible : Visibility.Collapsed;
}
public override object[] ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new System.NotImplementedException();
}
}
And add the converter as a page resource:
<Page.Resources>
<local:VisibilityMultiConverter x:Key="VisibilityMultiConverter" />
</Page.Resources>