在视图模型时设定的ListView的SelectedItem不突出 [英] ListView SelectedItem not highlighted when set in ViewModel

查看:132
本文介绍了在视图模型时设定的ListView的SelectedItem不突出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的ListView 的ItemSource 数据绑定和的SelectedItem 数据绑定。

I have a ListView with a ItemSource data binding and a SelectedItem data binding.

的ListView 填充了一个新的的ItemSource 我每次preSS下一步或previous按钮。

The ListView is populated with a new ItemSource every time I press the Next or Previous button.

的SelectedItem 相应地更新,在项目的ItemSource 已选定 的状态,所以可以当用户导航来回想起。

The SelectedItem is updated accordingly, the items in the ItemSource have the Selected state, so it can be remembered when the user navigates back and forth.

调试时,一切似乎都很好地工作。虚拟机更新为预期的控制,我也可以看到的ListView 具有正确的价值选择,当我下一个和previous按钮导航。

While debugging, everything seems to work perfectly. The VM updates the controls as expected, and I can also see that the ListView has the correct selected value when I navigate with the next and previous buttons.

问题是,无论是在ListView有一个正确的的SelectedItem 的ListView 没有事实可视化的SelectedItem 所强调的。

The problem is, that regardless of the fact that the ListView has a correct SelectedItem, the ListView does not visualize the SelectedItem as highlighted.

XAML:

<ListView 
    x:Name="_matchingTvShowsFromOnlineDatabaseListView" 
    Grid.Row="0" 
    Grid.Column="0"
    Grid.RowSpan="3"
    ItemsSource="{Binding AvailableMatchingTvShows}"
    SelectedItem="{Binding AcceptedMatchingTvShow, Mode=TwoWay}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Name}"/>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

在行为视图模型负责重新填充的ItemSource 的SelectedItem

private void UpdateForCurrentVisibleTvShow()
{
    var selectedTvShow = FoundTvShows[CurrentTvShow];

    // Update the available matches
    var availableMatchingTvShows = new ObservableCollection<IWebApiTvShow>();
    if (AvailableTvShowMatches[selectedTvShow] != null)
    {
        foreach (var webApiTvShow in AvailableTvShowMatches[selectedTvShow])
        {
            availableMatchingTvShows.Add(webApiTvShow);
        }
    }
    AvailableMatchingTvShows = availableMatchingTvShows;

    // Update the selected item
    AcceptedMatchingTvShow = availableMatchingTvShows.FirstOrDefault(webApiTvShow => webApiTvShow.Accepted);

    // Update the progress text
    CurrentTvShowInfoText = string.Format(
        "TV Show: {0} ({1} of {2} TV Shows)",
        FoundTvShows[CurrentTvShow],
        CurrentTvShow + 1,
        FoundTvShows.Count);

    // Update the AcceptedMatchingTvShow selection in the listview
    OnPropertyChanged("AcceptedMatchingTvShow");
}

的实施 AcceptedMatchingTvShow

public IWebApiTvShow AcceptedMatchingTvShow
{
    get
    {
        IWebApiTvShow acceptedTvShow = null;
        if (FoundTvShows.Count > 0)
        {
            var tvShowName = FoundTvShows[CurrentTvShow];
            acceptedTvShow = AvailableTvShowMatches[tvShowName].FirstOrDefault(webApiTvShow => webApiTvShow.Accepted);
        }
        return acceptedTvShow;
    }
    set
    {
        if (value != null)
        {
            var tvShowName = FoundTvShows[CurrentTvShow];
            var currentlyAcceptedTvShow =
                AvailableTvShowMatches[tvShowName].FirstOrDefault(webApiTvShow => webApiTvShow.Accepted);
            if (currentlyAcceptedTvShow != null)
            {
                currentlyAcceptedTvShow.Accepted = false;
            }
            value.Accepted = true;
        }
        OnPropertyChanged();
    }
}

我希望有人可以点我在正确的方向。只是要清楚,在ListView具有正确的项目,则selectedItem设置了正确的项目。

I hope somebody can point me in the right direction. Just to be clear, the ListView has the correct items, and the SelectedItem is set with the correct item.

推荐答案

好吧,我发现了很多调试和挖掘后,解决方案的问题。我真的想了解,如果这是WPF如何意味着控制的行为,如果这是在的ListView中的错误 s的数据绑定的一部分。如果有人能告诉我,我很很好奇,正确的答案(也许我在错误的方式解决了这个问题,有人可以解释我,我应该怎么做了这一点)。

Well, I found 'a solution' to the problem after a lot of debugging and digging. I would REALLY like to understand if this is how WPF meant the control to behave, or if this is a bug in the ListViews data binding part. If anyone could tell me that, I am very very curious to the correct answer (and maybe I solved this problem in the wrong way, and somebody could explain me how I should've done this).

不管怎样,问题似乎得到解决,当我创建的对象的复制

Anyway, the problem seems to be resolved when I create a copy of the object:

    public IWebApiTvShow AcceptedMatchingTvShow
    {
        get
        {
            IWebApiTvShow acceptedTvShow = null;
            if (FoundTvShows.Count > CurrentTvShow)
            {
                var tvShowName = FoundTvShows[CurrentTvShow];
                acceptedTvShow = AvailableTvShowMatches[tvShowName].FirstOrDefault(webApiTvShow => webApiTvShow.Accepted);
            }

            if (acceptedTvShow != null)
            {
                // I MUST create a new instance of the original object for the ListView to update the selected item (why??)
                return new WebApiTvShow(acceptedTvShow);
            }
            return null;
        }
        set
        {
            if (value != null)
            {
                var tvShowName = FoundTvShows[CurrentTvShow];
                var availableTvShowMatch = AvailableTvShowMatches[tvShowName];
                var currentlyAcceptedTvShow = availableTvShowMatch.FirstOrDefault(webApiTvShow => webApiTvShow.Accepted);
                if (currentlyAcceptedTvShow != null)
                {
                    currentlyAcceptedTvShow.Accepted = false;
                }
                value.Accepted = true;
            }

            OnPropertyChanged();
        }
    }

请注意调用拷贝构造函数:

Note the call to the copy constructor :

返回新WebApiTvShow(acceptedTvShow);

它的工作原理,但似乎真的可笑,气味像的ListView 来我的错误。是吗?

It works, but seems really ridiculous and smells like a bug in ListView to me. Is it?

我试图解释一个简单的例子同样的问题<一href=\"http://stackoverflow.com/questions/25919112/listview-remember-selecteditem-not-working-as-i-expected-a-bug-in-wpfs-listvi\">here,如果有人可以确认错误或可以解释我如何这应该已经实现,我将不胜AP preciate的见解。

I tried to explain the same problem in a simpler example here, if anybody can confirm the bug or can explain me how this should've been implemented I would greatly appreciate the insights.

这篇关于在视图模型时设定的ListView的SelectedItem不突出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆