ListView - select index only programmatically

会有一股神秘感。 提交于 2019-12-23 05:26:21

问题


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 :)


回答1:


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

In XAML:

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

And bounded property:

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



回答2:


You have two tasks here:

Selecting programmatically: WPF ListView Programmatically Select Item

And disabling user selection: WPF ListView turn off selection




回答3:


Just have no set

 Public Int SelectedIndex 
 {
      get { return selectedindex; }  
 }

 public void mysub()
 {
      selectedindex = 2; 
      NotifyPropertyChanged("SelectedIndex");
 }


来源:https://stackoverflow.com/questions/9837132/listview-select-index-only-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!