User avatar
Scherub
Topic Author
Posts: 141
Joined: 06 May 2014, 20:53
Contact:

[Unity] Binding of IsSelected-Property of ListBoxItem

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:
[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
}
<!-- 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}"/>
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.
 
User avatar
sfernandez
Site Admin
Posts: 1935
Joined: 22 Dec 2011, 19:20

Re: [Unity] Binding of IsSelected-Property of ListBoxItem

03 Sep 2014, 17:21

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:
<!-- 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>
 
User avatar
Scherub
Topic Author
Posts: 141
Joined: 06 May 2014, 20:53
Contact:

Re: [Unity] Binding of IsSelected-Property of ListBoxItem

03 Sep 2014, 19:06

That works! Thanks a lot! :)
 
User avatar
jsantos
Site Admin
Posts: 2922
Joined: 20 Jan 2012, 17:18
Contact:

Re: [Unity] Binding of IsSelected-Property of ListBoxItem

05 Sep 2014, 01:51

We fixed this in v1.1.11!
 
User avatar
Scherub
Topic Author
Posts: 141
Joined: 06 May 2014, 20:53
Contact:

Re: [Unity] Binding of IsSelected-Property of ListBoxItem

07 Sep 2014, 18:01

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.
[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
}
[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
}
 
User avatar
sfernandez
Site Admin
Posts: 1935
Joined: 22 Dec 2011, 19:20

Re: [Unity] Binding of IsSelected-Property of ListBoxItem

10 Sep 2014, 10:38

Hi,

I have a similar test here (without the RelayCommand) and IsSelected binding works correctly and is being updated as expected:
<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>
Could you please post which control are you using and how do you bind the IsSelected property?

Who is online

Users browsing this forum: Google [Bot] and 4 guests