WPF data bound ListView/ListVBox won't select if list only has one item
So this is pretty mystifying. I have a databound ListBox
(or ListView
, it happens with both), and if it has more than 2 items in it, selection works - I get a blue highlight bar and the item looks selected. If I only have 1 item, the selection does not work - I do not get a blue highlight bar, but the selection events all fire as normal. Is this a bug in WPF? Or am I just crazy开发者_StackOverflow社区?
This is how I'm setting up my ListBox
:
<ListView x:Name="plotListBox"
DockPanel.Dock="Bottom"
ItemsSource="{Binding Path=Plots}"
SelectedItem="{Binding Path=SelectedPlot, Mode=TwoWay}"
SelectionMode="Single"
ScrollViewer.CanContentScroll="False"/>
Has anyone else run into something like this? This post seems to describe a similar issue, but I can't find anything in the replies that I think would help my situation.
Try putting IsSynchronizedWithCurrentItem="True" on the ListView
I haven't ever had this problem before. I just used your XAML (but with a static array) in kaxaml and it worked fine. Then I pasted it to a new WPF project in VS2008 and it was fine. Here's my code-behind:
public partial class Window1 : Window { public ObservableCollection Plots { get; set; }
public Window1()
{
Plots = new ObservableCollection<string>();
InitializeComponent();
this.DataContext = this;
Plots.Add( "hello");
//Plots.Add( "world");
}
}
just having one item in the ObservableCollection didn't make selection break.
Wow, the culprit was ScrollViewer.CanContentScroll="False"
. Everything worked fine once I pulled that out.
精彩评论