2

我创建了一个应用程序,可以使用图像注册一些用户。为了列出它们,我在 a 中显示了这些图像ListBox,然后您可以单击每个用户图像来选择它,如图所示。

在此处输入图像描述

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>

ListBoxXAML:_

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

后面的代码仅ListBox使用ObservableCollection.

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

在此处输入图像描述

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

我不得不说我在另一个中使用相同的代码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();
    }

事件发生在 another Window,所以我认为问题可能不在这里。

4

0 回答 0