[Unity] Binding of IsSelected-Property of ListBoxItem
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}"/>
-
sfernandez
Site Admin
- Posts: 3184
- Joined:
Re: [Unity] Binding of IsSelected-Property of ListBoxItem
Hi,
Your code is correct. The problem is in our side. Right now we don't expose the data item (your class) in the context of the item container (the ListBoxItem in your case), so bindings made in the ItemContainerStyle can't work this way. We will fix it for the next release.
Meanwhile you should bind the properties through the container Content property:
Your code is correct. The problem is in our side. Right now we don't expose the data item (your class) in the context of the item container (the ListBoxItem in your case), so bindings made in the ItemContainerStyle can't work this way. We will fix it for the next release.
Meanwhile you should bind the properties through the container Content property:
Code: Select all
<!-- defined as resources in a resource dictionary -->
<Style x:Key="BuildingOptionItemContainerStyle" TargetType="ListBoxItem">
<Setter Property="IsSelected" Value="{Binding Content.DataContext.IsSelected,
Mode=TwoWay, RelativeSource={RelativeSource Self}}"/>
</Style>
Re: [Unity] Binding of IsSelected-Property of ListBoxItem
That works! Thanks a lot!
Re: [Unity] Binding of IsSelected-Property of ListBoxItem
We fixed this in v1.1.11!
Re: [Unity] Binding of IsSelected-Property of ListBoxItem
After upgrading to v1.1.11 and spending a few hours to fix a few things I can confirm that it works.
BUT there is another problem with class inheritance. I'll try to give you a short example:
I introduced a simple base class BaseMenuEntry that does more or less the same as the old BuildMenuOption. The BuildMenuOption class now inherits from BaseMenuEntry and has an additional property. The problem is that the IsSelected-binding doesn't work again any more. So everything works when it is in the same class but it doesn't work when it's not directly derived from BaseComponent.
BUT there is another problem with class inheritance. I'll try to give you a short example:
I introduced a simple base class BaseMenuEntry that does more or less the same as the old BuildMenuOption. The BuildMenuOption class now inherits from BaseMenuEntry and has an additional property. The problem is that the IsSelected-binding doesn't work again any more. So everything works when it is in the same class but it doesn't work when it's not directly derived from BaseComponent.
Code: Select all
[Extended]
public class BaseMenuEntry : BaseComponent
{
#region Properties
public string Name { get; private set; }
public RelayCommand Command { get; private set; }
bool _isSelected;
public bool IsSelected
{
get { return _isSelected; }
set
{
if (value != _isSelected)
{
_isSelected = value;
NotifyPropertyChanged("IsSelected");
if (value)
{
if (Command != null && Command.CanExecute(this))
Command.Execute(this);
}
}
}
}
#endregion
#region Constructor
public BaseMenuEntry(string name, RelayCommand command)
{
Name = name;
Command = command;
}
#endregion
}
Code: Select all
[Extended]
public class BuildMenuOption : BaseMenuEntry
{
#region Properties
public BuildTools BuildTool { get; private set; }
#endregion
#region Constructor
public BuildMenuOption(BuildTools buildTool, string name, RelayCommand command)
: base(name, command)
{
BuildTool = buildTool;
}
#endregion
}
-
sfernandez
Site Admin
- Posts: 3184
- Joined:
Re: [Unity] Binding of IsSelected-Property of ListBoxItem
Hi,
I have a similar test here (without the RelayCommand) and IsSelected binding works correctly and is being updated as expected:
Could you please post which control are you using and how do you bind the IsSelected property?
I have a similar test here (without the RelayCommand) and IsSelected binding works correctly and is being updated as expected:
Code: Select all
<Grid
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ListBox x:Name="lb" HorizontalAlignment="Center" VerticalAlignment="Center">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" FontSize="40"/>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="IsSelected" Value="{Binding IsSelected}"/>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
<Button x:Name="btn" Content="Select Start" Width="100" Height="40" Margin="50" VerticalAlignment="Bottom"/>
</Grid>
Who is online
Users browsing this forum: Ahrefs [Bot] and 6 guests