问题
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