James Joshua Street
James Joshua Street

Reputation: 3409

WPF ItemsControl resetting IsSelected to false when I select a new treeviewitem

I extended ItemsControl to allow MultiSelection.
I bind the data OneWayToSource to a viewmodel.

The containers all have their IsSelected property set to true based on the mouse event.

I see the data propagate from the container to the viewmodel, and IsSelected updates. When I step through the code, immediately after it completes on PropertyChanged, it immediate run the setter a second time setting the property back to false. I'm so confused as to what is happening. Right before resetting the property back to false it steps through this non-user code.

Step into: Stepping over non-user code 'System.ComponentModel.PropertyChangedEventArgs.PropertyChangedEventArgs'
Step into: Stepping over non-user code 'MS.Internal.Data.PropertyPathWorker.GetValue'
Step into: Stepping over non-user code 'MS.Internal.Data.PropertyPathWorker.RawValue'
Step into: Stepping over non-user code 'MS.Internal.Data.PropertyPathWorker.RawValue'
Step into: Stepping over non-user code 'MS.Internal.Data.ClrBindingWorker.RawValue'
Step into: Stepping over non-user code 'System.Windows.Data.BindingExpression.TransferValue'

bool _IsSelected;
public bool IsSelected
{
    get
    {
        return _IsSelected;
    }
    set
    {
        if (value != _IsSelected)
        {
            _IsSelected = (bool)value;
            OnPropertyChanged("IsSelected");
        }
    }
}


protected void OnPropertyChanged(string name)
{
    PropertyChangedEventHandler handler = PropertyChanged;
    if (handler != null)
    {
        handler(this, new PropertyChangedEventArgs(name));
    }
}

I swear I never remember to say everything in this post. I'm extending items control to make a multiselect treeview. I actually mostly just used the code i found in someone else's and adapted it to allow keyboard navigation.

        <Rectangle
                   x:Name="Rectangle" Fill="Transparent" Stroke="Black" 
                                   StrokeDashCap="Square" StrokeThickness="0" SnapsToDevicePixels="True">
          <Rectangle.StrokeDashArray>
            <sys:Double>5</sys:Double>
          </Rectangle.StrokeDashArray>
        </Rectangle>
        <Border Name="Bd"
                  Background="{TemplateBinding Background}"
                  BorderBrush="{TemplateBinding BorderBrush}"
                  BorderThickness="{TemplateBinding BorderThickness}"
                  Padding="{TemplateBinding Padding}"
                  >
          <ContentPresenter x:Name="PART_Header"
                              ContentSource="Header"
                              HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                              />
        </Border>
        <ItemsPresenter 
          x:Name="ItemsHost"
          Grid.Row="1"
          />
      </Grid>
      <ControlTemplate.Triggers>
        <Trigger Property="IsExpanded" Value="false">
          <Setter TargetName="ItemsHost"
                    Property="Visibility"
                    Value="Collapsed"
                    />
        </Trigger>

        <MultiTrigger>
          <MultiTrigger.Conditions>
            <Condition Property="HasHeader"
               Value="false"/>
            <Condition Property="Width"
               Value="Auto"/>
          </MultiTrigger.Conditions>
          <Setter TargetName="PART_Header"
                    Property="MinWidth"
                    Value="75"
                    />
        </MultiTrigger>
        <MultiTrigger>
          <MultiTrigger.Conditions>
            <Condition Property="HasHeader"
                       Value="false"/>
            <Condition Property="Height"
                       Value="Auto"/>
          </MultiTrigger.Conditions>
          <Setter TargetName="PART_Header"
                    Property="MinHeight"
                    Value="19"/>
        </MultiTrigger>
        <Trigger Property="IsSelected"
                   Value="true">
          <Setter TargetName="ItemMainGrid"
                    Property="Background"
                    Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
          <Setter Property="Foreground"
                    Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
        </Trigger>
        <MultiTrigger>
          <MultiTrigger.Conditions>
            <Condition Property="IsOutlined"
                       Value="true" 
                       />
            <Condition Property="IsSelected"
                       Value="false" 
                       />
          </MultiTrigger.Conditions>
         <Setter TargetName="Rectangle" Property="StrokeThickness" Value="1" />
        </MultiTrigger>        
        <Trigger Property="IsOutlined"
                   Value="false">
          <Setter TargetName="Rectangle" Property="StrokeThickness" Value="0" />
        </Trigger>
        <Trigger Property="IsEnabled"
                   Value="false">
          <Setter Property="Foreground"
                    Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
        </Trigger>
      </ControlTemplate.Triggers>
    </ControlTemplate>
  </Setter.Value>
</Setter>

Anyway I will figure it out eventually. I thought i remembered the default wpf treeview not allowing multiselect and actually deselecting all items if you tried to set isselected on multiple treeviewitems, so I was wondering if maybe itemscontrol had that kind of behavior, even though it doesn't make sense for a generic itemscontrol to mess with selection. Not sure what is going on but i will figure it out eventually

Upvotes: 0

Views: 671

Answers (2)

James Joshua Street
James Joshua Street

Reputation: 3409

finally found it. just a stupid place where i had another binding set wrong. Interacting bindings cause weird behavior. Most likely an update of one triggering a cycle that turned it off again.

Sorry that i waste so many people's time with silly questions. But often I just want confirmation that things work the way i think they do. I have no faith in anything when my code isn't working

Upvotes: 0

dev hedgehog
dev hedgehog

Reputation: 8791

ItemsControl doesn't allow any selection by design so I have no idea what you mean with IsSelected being changed when ItemsControl selects an item. That thing doesnt select at all.

Furthermore if you want multi-selection behavior you can use ListBox and change SelectionMode to Multiple or Extended.

Check this link out:

http://msdn.microsoft.com/en-us/library/system.windows.controls.listbox.selectionmode.aspx

Upvotes: 1

Related Questions