2017-05-31 97 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