简体   繁体   English

当我选择新的treeviewitem时,WPF ItemsControl将IsSelected重置为false

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

I extended ItemsControl to allow MultiSelection . 我扩展了ItemsControl以允许MultiSelection
I bind the data OneWayToSource to a viewmodel. 我将数据OneWayToSource绑定到视图模型。

The containers all have their IsSelected property set to true based on the mouse event. 容器都基于鼠标事件将其IsSelected属性设置为true。

I see the data propagate from the container to the viewmodel, and IsSelected updates. 我看到数据从容器传播到视图模型,并且IsSelected更新。 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. 当我单步执行代码时,在PropertyChanged完成代码后,它将立即立即第二次运行setter,将属性设置为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. 在将属性重置为false之前,它会逐步浏览此非用户代码。

Step into: Stepping over non-user code 'System.ComponentModel.PropertyChangedEventArgs.PropertyChangedEventArgs' 进入:单步执行非用户代码'System.ComponentModel.PropertyChangedEventArgs.PropertyChangedEventArgs'
Step into: Stepping over non-user code 'MS.Internal.Data.PropertyPathWorker.GetValue' 涉足:遍历非用户代码“ MS.Internal.Data.PropertyPathWorker.GetValue”
Step into: Stepping over non-user code 'MS.Internal.Data.PropertyPathWorker.RawValue' 涉足:遍历非用户代码“ MS.Internal.Data.PropertyPathWorker.RawValue”
Step into: Stepping over non-user code 'MS.Internal.Data.PropertyPathWorker.RawValue' 涉足:遍历非用户代码“ MS.Internal.Data.PropertyPathWorker.RawValue”
Step into: Stepping over non-user code 'MS.Internal.Data.ClrBindingWorker.RawValue' 介入:逐步解决非用户代码“ MS.Internal.Data.ClrBindingWorker.RawValue”
Step into: Stepping over non-user code 'System.Windows.Data.BindingExpression.TransferValue' 进入:遍历非用户代码'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. 我以为我记得默认的wpf树视图不允许多重选择,并且如果您尝试在多个treeviewitems上设置isselect,则实际上取消选择所有项目,所以我想知道itemscontrol是否具有这种行为,即使对于通用而言这没有意义itemscontrol使选择混乱。 Not sure what is going on but i will figure it out eventually 不知道发生了什么,但我最终会弄清楚

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. ItemsControl不允许通过设计进行任何选择,因此我不知道在ItemsControl选择项目时更改IsSelected含义。 That thing doesnt select at all. 那东西根本没有选择。

Furthermore if you want multi-selection behavior you can use ListBox and change SelectionMode to Multiple or Extended . 此外,如果您想要多选行为,则可以使用ListBox并将SelectionMode更改为MultipleExtended

Check this link out: 检查此链接:

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

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 当我的代码不起作用时,我对任何事情都不信任

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM