ListView-仅以编程方式选择索引 [英] ListView - select index only programmatically

查看:78
本文介绍了ListView-仅以编程方式选择索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现列表框(或列表视图):

i am trying to implement listbox (or listview):

<ListView ItemsSource="{Binding Players}" SelectedIndex="{Binding SelectedIndex}">

我的问题是,我想将所选索引绑定到代码隐藏的属性中. 它仅在表单开始时起作用,但我需要禁用用户才能更改选择.选择素将仅通过编程方式更改.

My problem is, that i want to bind selected index to property in code-behind. It work only on form start, but i need to disable user to change selection. Selectin will be changed ONLY programmaticaly.

感谢所有建议或解决方案:)

Thanks for all advices or solutions :)

推荐答案

因此,可行的解决方案:

So, working solution:

private void playersList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (sender.GetType() == typeof(ListView))
    {
        (sender as ListView).SelectedIndex = GameObserver.Instance.core.SelectedIndex;
        e.Handled = true;
    }
}

在XAML中:

<ListView ItemsSource="{Binding Players}" SelectedIndex="{Binding SelectedIndex}" SelectionChanged="playersList_SelectionChanged">

和有界属性:

private int selectedIndex = 1;
public int SelectedIndex
{
    get
    {
        return selectedIndex;
    }
}

这篇关于ListView-仅以编程方式选择索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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