簡體   English   中英

從ViewModel更改ListBox的SelectedItem

[英]Change SelectedItem of ListBox from ViewModel

我怎樣才能獲得SelectedItem一個的ListBox設置后要強調SelectedItem在視圖模型?

ItemsSource綁定到BarObservableCollection (該集合是類Foo的成員。一個按鈕綁定到一個命令,該命令將新的空Bar實例添加到集合中,然后還將SelectedItem設置為新的空實例。

將實例添加到集合后, ListBox將更新以顯示新的空白Bar 但是,在ViewModel中設置SelectedItem屬性后,新實例不會在ListBox突出顯示,但它正在設置並且引發了PropertyChanged事件( SelectedItem顯示在View中的其他位置)。

額外細節:

INotifyPropertyChanged在基礎ViewModel類中實現,也在FooBar類中實現。

ListBox包含一個自定義ItemTemplate以顯示Bar成員,以及一個自定義ItemContainerStyle ,用於修改IsMouseOver觸發器的Background

簡化的xaml:

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

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

簡化的viewmodel:

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;
}

您的代碼對我來說非常合適。

在此輸入圖像描述

請注意,“Bar 3” 選中,但是當列表框沒有焦點時,Windows 7上的默認主題非常難以阻止您注意。 Windows 7甚至不是最糟糕的一群。 我們的應用程序使用WhiteSmoke作為默認背景,我們遇到了主要問題,老用戶1無法判斷是否選擇了列表框項目。

幸運的是,這是一個微不足道的修復。 這些資源可以很容易地存在於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>

1 “較舊”意味着足夠投票。


如果您的.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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM