简体   繁体   English

ListView在启动时无法突出显示所选项目

[英]ListView fails to highlight selected item on start

I'm currently struggling with very weird issue and cannot find the cause of it. 我目前正在为一个很奇怪的问题而苦苦挣扎,找不到原因。 I have a ListView : 我有一个ListView

 <ListView ItemsSource="{Binding AvailableTestCaseDatas}" Grid.Row="2" Name="GListView" SelectedItem="{Binding SelectedTestCaseData, Mode=TwoWay}">
         <ListView.Resources>
            <Style TargetType="ListViewItem">
                <Style.Resources>
                    <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="{x:Static SystemColors.HighlightColor}"/>
                </Style.Resources>
            </Style>
        </ListView.Resources>
        <ListView.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding TestCaseName}"/>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

But when it loads, I do not see the item selected, even though I'm pretty sure both bindings are correct. 但是,在加载时,即使我确定两个绑定都正确,我也看不到选定的项目。 Furthermore, the item appears to be selected internally, but is not highlighted by ListView, because when I click on the selected item, ListView does not highlight it. 此外,该项目似乎是在内部选择的,但不会由ListView突出显示,因为当我单击所选项目时,ListView不会突出显示它。 When I click on the other item in the list - the clicked item is highlighted, and then I can select the item that was selected on the start. 当我单击列表中的另一个项目时-单击的项目将突出显示,然后我可以选择开始时选择的项目。

The issue can be reproduced, even if I remove SelectedItem from XAML, and try to do listView.SelectedIndex = 0 on load event. 即使我从XAML中删除SelectedItem ,并尝试在加载事件上执行listView.SelectedIndex = 0 ,也可以重现此问题。 It still does not highlight the selected item, even though it is selected and I'm pretty sure that: 即使选中了它,它仍然不会突出显示选中的项目,我很确定:

  1. list.Items.Contains(list.SelectedItem); list.Items.Contains(List.selectedItem单选);
  2. list.Items.Count > 0 list.Items.Count> 0

Also when I do this: 另外,当我这样做时:

ctor {

        listView.Loaded += ListViewLoaded;
}

    void ListViewLoaded(object sender, System.Windows.RoutedEventArgs e)
    {
        var item = listView.SelectedItem;
        listView.SelectedIndex = -1;
        listView.SelectedItem = item;
    }

Which looks extremely weird, it starts to work! 看起来很奇怪,它开始起作用!

My binding: 我的绑定:

    public TestCaseData SelectedTestCaseData
    {
        get { return _selectedTestCaseData; }
        set
        {
            _selectedTestCaseData = value;
            OnPropertyChanged("SelectedTestCaseData");
        }
    }

I tried: 我试过了:

  1. Removing bindings, and checking the issue with the code. 删除绑定,并检查代码问题。 Still reproduced. 仍转载。
  2. Ensured that no other references are put in the code to this list 确保此列表的代码中没有其他引用
  3. Making the list enabled (it is disabled from the start) 启用列表(从一开始就将其禁用)
  4. Ensured that list.Items.Contains(list.SelectedItem) && list.Items.Count > 0 确保list.Items.Contains(list.SelectedItem)&& list.Items.Count> 0
  5. Removing style and datatemplate. 删除样式和数据模板。

It works if I put this Loaded event, but it is a hack, and I would like at least a reason for why it behaves like that. 如果我放置了此Loaded事件,它会起作用,但它是一个hack,我至少希望知道为什么它具有这种行为。 Any tips? 有小费吗?

I tried your Xaml code with some data and works fine, so the problem is in the way you are setting the property the first. 我用一些数据尝试了您的Xaml代码,并且工作正常,所以问题出在您首先设置属性的方式上。 The most probably issue is that the SelectedItem is been selected and unselected in the first time, something odd is happening there. 最可能的问题是, SelectedItem是在第一次被选择和未选择的,在那里发生了奇怪的事情。 I suggest you to show the code when you set the selected item (your SelectedTestCaseData ) and also the code when set the data context and when you are loading the collection data. 我建议您在设置所选项目(您的SelectedTestCaseData )时显示代码,并在设置数据上下文以及加载集合数据时显示代码。 Here I will show my code that works, maybe it helps: 在这里,我将展示有效的代码,也许有帮助:

Same abouve XAML but with my variables: 与XAML相同,但有我的变量:

        <ListView ItemsSource="{Binding Persons}" Grid.Row="2" Name="GListView" SelectedItem="{Binding SelectedPerson, Mode=TwoWay}">
            <ListView.Resources>
                <Style TargetType="ListViewItem">
                    <Style.Resources>
                        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="{x:Static SystemColors.HighlightColor}"/>
                    </Style.Resources>
                </Style>
            </ListView.Resources>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}"/>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

The model: 该模型:

public class PersonViewModel : ViewModelBase
{
    #region Name

    private string _Name;
    private const string NameName = "Name";

    public string Name
    {
        get { return _Name; }
        set
        {
            Set(NameName, ref _Name, value);
        }
    }

    #endregion
}

The properties in the MainViewModel datacontext: MainViewModel数据上下文中的属性:

    #region Persons

    private ObservableCollection<PersonViewModel> _Persons;

    public ObservableCollection<PersonViewModel> Persons
    {
        get { return _Persons ?? (_Persons = GetAllPersons()); }
    }

    private ObservableCollection<PersonViewModel> GetAllPersons()
    {
        var toRet = new ObservableCollection<PersonViewModel>();

        foreach (var i in Enumerable.Range(1,10))
        {
            toRet.Add(new PersonViewModel {Name = string.Format("Person Name {0}", i)});
        }

        //!!!!!!!!!!!!!!!!!!!!!HERE I SET THE SELECTED ITEM
        SelectedPerson = toRet.First();

        return toRet;
    }


    #endregion

    #region SelectedPerson

    private PersonViewModel _SelectedPerson;
    private const string SelectedPersonName = "SelectedPerson";

    public PersonViewModel SelectedPerson
    {
        get { return _SelectedPerson; }
        set
        {
            Set(SelectedPersonName, ref _SelectedPerson, value);
        }
    }

    #endregion

The way the data context is set 设置数据上下文的方式

<Window x:Class="..."
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    ...
    DataContext="{Binding Main, Source={StaticResource Locator}}">

In this case I'm user the Mvvm ligth's Locator for setting the window's data context. 在这种情况下,我使用Mvvm ligth的Locator来设置窗口的数据上下文。 Hope this helps to solve this issue... 希望这有助于解决此问题...

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

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