0

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

prespic
  • 1,635
  • 1
  • 17
  • 20

3 Answers3

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;
    }
}
prespic
  • 1,635
  • 1
  • 17
  • 20
0

You have two tasks here:

Selecting programmatically: WPF ListView Programmatically Select Item

And disabling user selection: WPF ListView turn off selection

Community
  • 1
  • 1
  • I didn't specified what i want to help exacly, ma fault. About the selection disabling, i need just disable user to change selection, but this solution is how to disable selection at all (concrete, how to change backround like item is not selected). – prespic Mar 23 '12 at 10:44
  • You specified just fine, don't worry. Hope my links helped. –  Mar 23 '12 at 10:45
  • I need just disable user to change selection, but this solution is how to disable selection at all (concrete, how to change backround like item is not selected). NOTE: This is edit from first answer... – prespic Mar 23 '12 at 10:49
  • Just take the previously selected index and revert it back once the user has made a new selection if what you're saying is that you want it to appear to not be selected. I'm not sure I understand what you're asking now though sorry. –  Mar 23 '12 at 10:55
  • private void playersList_SelectionChanged(object sender, SelectionChangedEventArgs e) { Console.WriteLine(sender.GetType().ToString()); if (e != null) { (sender as ListBox).SelectedItem = e.RemovedItems[0]; } } But this effect is stack overflow... – prespic Mar 23 '12 at 11:06
  • The question's getting sloppy now. You need to be clear about what you're asking. –  Mar 23 '12 at 11:18
  • Now, i'm trying what you wrote before. It looks like it will work. Thank a lot for tips. I will post result, if i find it. – prespic Mar 23 '12 at 11:25
-1

Just have no set

 Public Int SelectedIndex 
 {
      get { return selectedindex; }  
 }

 public void mysub()
 {
      selectedindex = 2; 
      NotifyPropertyChanged("SelectedIndex");
 }
paparazzo
  • 44,497
  • 23
  • 105
  • 176
  • Yes, this is exacly how i did it. But the problem is, that user can change selection by "click" on list. I need the only way to change is by my code... – prespic Mar 23 '12 at 10:54
  • You did not include that detail in your question. You should be marking down your question for lack of showing what you tried rather than marking down the answer. That answer does stop the SelectedIndex from being set. The visual is another matter. – paparazzo Mar 23 '12 at 12:28