2017-05-31 99 views
1

在ViewModel中设置SelectedItem后,如何才能使ListBoxSelectedItem高亮显示?从ViewModel更改列表框的SelectedItem

ItemsSource势必BarObservableCollection(集合是一类Foo的成员。A按钮被绑定到,增加了一个新的空Bar实例的集合,然后也设置SelectedItem到新的空的命令实例。

将该实例添加到集合后,ListBox被更新以显示新的空白Bar。但是,设置在视图模型的SelectedItem财产之后,新的实例未在ListBox突出,但它被设置并提出PropertyChanged事件(SelectedItem显示在视图的其他地方)。

其他详情:

INotifyPropertyChanged在碱ViewModel类实现,并且在FooBar类也实现。

ListBox包含自定义ItemTemplate显示Bar成员,并且修改该BackgroundIsMouseOver触发定制ItemContainerStyle

简化XAML:

<ListBox ItemsSource="{Binding Path=MyFoo.BarCollection}" 
     SelectedItem="{Binding Path=SelectedItem, 
         UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/> 

<Button Content="Add New Bar" 
     Command="{Binding Path=AddBarCommand}"/> 

简化视图模型:

private Foo _myFoo; 
public Foo MyFoo 
{ 
    get { return _myFoo; } 
    set { _myFoo= value; OnPropertyChanged("MyFoo"); } 
} 

private Bar _selectedItem; 
public Bar SelectedItem 
{ 
    get { return _selectedItem; } 
    set { _selectedItem = value; OnPropertyChanged("SelectedItem"); } 
} 

private void AddBar() 
{ 
    Bar newBar = new Bar(); 

    MyFoo.BarCollection.Add(newBar); 
    SelectedItem = newBar ; 
    _unsavedChanges = true; 
} 
+0

什么是'BarCollection'的类型? –

+0

@Ed'BarCollection'是一个'ObservableCollection ' – jonmicjam

+0

我认为它必须是。有了这个假设,你的代码对我来说工作得很好。是否有可能新的项目以非常浅的灰色突出显示,与周围的窗口几乎没有区别?现代版本的Windows中默认的非聚焦高亮背景色可能难以注意。如果是这种情况,它很容易解决。 –

回答

0

你的代码工作完美的我。

enter image description here

注意“栏3” 选择,但是当列表框没有焦点,在Windows默认主题7作品相当困难,让你注意不到。而Windows 7甚至不是最糟糕的一群。我们的应用程序使用WhiteSmoke作为默认背景,并且我们遇到了与较老用户有关的主要问题无法确定是否选择了列表框项目。

幸运的是,这是一个微不足道的修复。这些资源可能很容易在Window.Resources,全球ListBox风格或App.xaml。这取决于你想要应用它们的程度。

<ListBox 
    ItemsSource="{Binding MyFoo.BarCollection}" 
    SelectedItem="{Binding SelectedItem}" 
    > 
    <ListBox.Resources> 
     <SolidColorBrush 
      x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" 
      Color="{x:Static SystemColors.HighlightColor}" 
      Opacity="0.5" 
      /> 
     <SolidColorBrush 
      x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}" 
      Color="{x:Static SystemColors.HighlightTextColor}" 
      /> 
    </ListBox.Resources> 
</ListBox> 

“老年”,意思是足够老投票。


如果你的.NET版本早的SystemColors.InactiveSelectionHighlightTextBrushKey存在,这很可能使用一些细化,但它的工作原理:

<ListBox 
    > 
    <ListBox.ItemContainerStyle> 
     <Style 
      TargetType="ListBoxItem" 
      BasedOn="{StaticResource {x:Type ListBoxItem}}" 
      > 
      <Style.Setters> 
       <Setter Property="Template"> 
        <Setter.Value> 
         <ControlTemplate TargetType="ListBoxItem"> 
          <Grid 
           Background="{TemplateBinding Background}" 
           > 
           <ContentPresenter /> 
          </Grid> 
         </ControlTemplate> 
        </Setter.Value> 
       </Setter> 
      </Style.Setters> 
      <Style.Triggers> 
       <Trigger Property="IsSelected" Value="True"> 
        <Setter 
         Property="Background" 
         Value="{StaticResource {x:Static SystemColors.HighlightBrushKey}}" 
         /> 
        <Setter 
         Property="Foreground" 
         Value="{StaticResource {x:Static SystemColors.HighlightTextBrushKey}}" 
         /> 
       </Trigger> 
       <MultiTrigger> 
        <MultiTrigger.Conditions> 
         <Condition Property="IsSelected" Value="True" /> 
         <Condition Property="IsEnabled" Value="False" /> 
        </MultiTrigger.Conditions> 
        <Setter 
         Property="Background" 
         Value="{StaticResource {x:Static SystemColors.InactiveCaptionBrushKey}}" 
         /> 
       </MultiTrigger> 
      </Style.Triggers> 
     </Style> 
    </ListBox.ItemContainerStyle> 
</ListBox> 
+0

正是我遇到的问题。感谢修复!标记为答案。 (抱歉,不知道+1) – jonmicjam

+0

NP,很高兴帮助。 –

+0

重写'InactiveSelectionHighlightBrushKey'并没有诀窍,尽管它在VS2010中破坏了Designer。 [Microsoft文档](https://msdn.microsoft.com/en-us/library/system.windows.systemcolors.inactiveselectionhighlightbrushkey(v = vs.110).aspx)也表示,“笔刷的IsFrozen属性是* * true **,所以不能修改。“这有什么好担心的吗? – jonmicjam