防止在ListView中更改选择

时间:2015-02-28 09:27:55

标签: c# xaml listview selection

我有一个ListView。我想阻止改变选择。

<ListView SelectionMode="Single" MinHeight="400" ItemsSource="{Binding Partners}" SelectedItem="{Binding SelectedPartner, Mode=TwoWay}">
    <ListView.View>
        <GridView>
             <Header="ID" DisplayMemberBinding="{Binding partnerId}"/>             
        </GridView>
    </ListView.View>
</ListView>

我在ViewModel:

public Partner SelectedPartner
{
    get { return _selectedPartner; }
    set
    {
        if (_selectedPartner == null) //when it isn't selected I select to new value
        {
            _selectedPartner = value;
            NotifyPropertyChanged("SelectedPartner");
        }
        else // when it is selected I don't change value and I want to do not change highlighting in view
        {
            NotifyPropertyChanged("SelectedPartner");
        }
    }
}

它被正确地调用,但新/旧选择的突出显示仍然有所改变。

1 个答案:

答案 0 :(得分:0)

我认为最简单的解决方案是在用户从IsEnabled中选择项目之后将属性false设置为ListView

<ListView SelectionMode="Single" MinHeight="400" ItemsSource="{Binding Partners}" 
          SelectedItem="{Binding SelectedPartner, Mode=TwoWay}">
    <ListView.Style>
        <Style TargetType="ListView">
            <Setter Property="IsEnabled" Value="False"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding SelectedPartner}" Value="{x:Null}">
                    <Setter Property="IsEnabled" Value="True"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ListView.Style>
    <ListView.View>
       ...
    </ListView.View>
</ListView>