当前位置: 动力学知识库 > 问答 > 编程问答 >

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?

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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: