[Unity] Binding of IsSelected-Property of ListBoxItem
Posted: 02 Sep 2014, 13:44
Hello,
I just came across the problem that apparently I cannot bind the IsSelected property of a ListBoxItem to a custom property of my class. I have a collection of items that gets displayed but when I select an item it doesn't change the bound property.
I have at least one workaround in mind that should work but it would be more convenient to use it the way I planned to. Can anyone confirm this as a known bug or tell me what I am doing wrong?
Following a few code snippets:
Oh, just one more thing. The IsSelected-Property of the ListBoxItem works for me in a trigger. So it seems it's just the binding that doesn't work for some reason.
I just came across the problem that apparently I cannot bind the IsSelected property of a ListBoxItem to a custom property of my class. I have a collection of items that gets displayed but when I select an item it doesn't change the bound property.
I have at least one workaround in mind that should work but it would be more convenient to use it the way I planned to. Can anyone confirm this as a known bug or tell me what I am doing wrong?
Following a few code snippets:
Code: Select all
[Extended]
public class BuildMenuOption : BaseComponent
{
public string Name { get; private set; }
bool _isSelected;
public bool IsSelected
{
get { return _isSelected; }
set
{
if (value != _isSelected)
{
_isSelected = value;
NotifyPropertyChanged("IsSelected");
}
}
}
// various other properties
}
Code: Select all
<!-- defined as resources in a resource dictionary -->
<Style x:Key="BuildingOptionItemContainerStyle" TargetType="ListBoxItem">
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
</Style>
<DataTemplate x:Key="BuildingOptionDataTemplate">
<Border Width="40" Height="40" Background="Gray" BorderBrush="Black">
<TextBlock Text="{Binding Name}" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>
</DataTemplate>
<!-- XAML in view -->
<ListBox ItemsSource="{Binding BuildingOptions.Items, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:BuildingMenuView}}}"
ItemContainerStyle="{StaticResource BuildingOptionItemContainerStyle}"
ItemTemplate="{StaticResource BuildingOptionDataTemplate}"/>