WPF中ListBox的奇怪行为

时间:2013-08-28 15:18:08

标签: c# wpf listbox wpf-controls

我创建了一个能够向某些用户注册图像的应用程序。要列出它们,我会在ListBox内显示图像,然后您可以点击每个用户图像进行选择,如此图所示。

enter image description here

ListBox正在使用我自己的Template

<Style TargetType="ListBox" x:Key="TiledListBox">
    <Setter Property="Margin" Value="0,0,10,0"></Setter>
    <Setter Property="Padding" Value="1"/>
    <Setter Property="Background" Value="White" />
    <Setter Property="HorizontalContentAlignment" Value="Left" />
    <Setter Property="VerticalContentAlignment" Value="Top" />
    <Setter Property="IsTabStop" Value="False" />
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <WrapPanel Width="{Binding Path=ActualWidth, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Grid}}}" />
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="ItemTemplate">
        <Setter.Value>
            <DataTemplate>
                <StackPanel Orientation="Vertical" Width="80">
                    <Image Source="{Binding Path=Picture}" Width="80" Height="80"></Image>
                </StackPanel>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

ListBox中的XAML

<ListBox Name="ListBoxUsers" ItemsSource="{Binding Path=ListObservableUsers, ElementName=SelectUser}" Style="{DynamicResource TiledListBox}" 
                 ScrollViewer.HorizontalScrollBarVisibility="Hidden" MouseDoubleClick="ListBoxUsers_MouseDoubleClick" />

后面的代码仅使用ListBox填充ObservableCollection个项目。

问题来了。当我尝试点击与第一个用户不同的用户时,会选择多个用户,如下图所示:

enter image description here

关于为什么会发生这种情况的任何想法?

我必须说我在另一个Window中使用了相同的代码并且它完美无缺,但我看不出它们之间有任何区别......

编辑:您可以使用此代码来使示例正常工作。

 public partial class MainWindow
{
    public ObservableCollection<DtoObject> ListObservableUsers { get; set; } 
    public MainWindow()
    {
        ListObservableUsers = new ObservableCollection<DtoObject>();
        InitializeComponent();

        Image image = new Image {Source = Extensions.LoadBitmap(new Bitmap(Properties.Resources.vaca))};
        for (int i = 0; i < 4; i++)
        {
            DtoObject dtoObject = new DtoObject {Picture = image};
            ListObservableUsers.Add(dtoObject);
        }
    }
}

public static class Extensions
{
    public static BitmapSource LoadBitmap(Bitmap source)
    {
        IntPtr ip = source.GetHbitmap();
        BitmapSource bs = null;
        bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip,
        IntPtr.Zero, Int32Rect.Empty,
        BitmapSizeOptions.FromEmptyOptions());

        return bs;
    }
}

public class DtoObject
{
    public Image Picture { get; set; }
}

private void ListBoxUsers_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    AcceptUser();
}    

private void AcceptUser()
    {
        if (ListBoxUsers.SelectedIndex < 0) return;

        selectedUser = (DtoObject)ListBoxUsers.SelectedItem;

        EventHandler handler = UserSelected;

        if (handler != null)
            handler(this, new EventArgs());

        DialogResult = true;
        Close();
    }

该事件进入另一个Window,所以我认为问题可能不在这里。

0 个答案:

没有答案