簡體   English   中英

列表框中更多突出顯示的項目

[英]More highlighted items in a listbox

我有一個ListBox ,其中放置了數據。 簡單的字符串,沒有什么極端的。 但是,用戶選擇數據將是什么,並且他可以和兩個具有相同名稱的不同(!!!)對象。

示例:它與圖片連接一起使用。 每張照片都有名字。 用戶選擇圖片並將其添加到列表框中。 但是,如果他選擇了兩張同名的圖片,則會在列表框中選擇項目:

圖片

為了避免這種情況該怎么辦? 我只需要一個突出顯示和選擇的項目。 該列表框設置為單個選擇,並且在選擇事件中,它表示僅選擇了一項。 因此,它僅涉及亮點。 (使用WPF,C#)

為了避免這種情況,您必須在字符串周圍使用包裝器。 您的圖片對象似乎是一個好的開始。

這里是一個示例,說明了兩種方法:

XAML:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        xmlns:local="clr-namespace:WpfApplication1">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <ListBox x:Name="list1" ItemsSource="{Binding Pictures1}" />
        <ListBox x:Name="list2" ItemsSource="{Binding Pictures2}" Grid.Column="1" DisplayMemberPath="Name" />
        <TextBox Text="{Binding Text}" Grid.Row="1"/>
        <Button Content="+" Grid.Row="1" Grid.Column="1" Click="Button_Click"/>
    </Grid>
</Window>

后面的代碼:

using System.Windows;
using System.Windows.Media;
using System.Windows.Input;
using System.Windows.Controls;
using System.Collections.ObjectModel;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public class Picture
        {
            public string Name { get; set; }
        }

        public string Text { get; set; }
        public ObservableCollection<string> Pictures1 { get; set; }
        public ObservableCollection<Picture> Pictures2 { get; set; }

        public MainWindow()
        {
            InitializeComponent();

            Pictures1 = new ObservableCollection<string>();
            Pictures2 = new ObservableCollection<Picture>();

            DataContext = this;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Pictures1.Add(Text);
            Pictures2.Add(new Picture { Name = Text });

            list1.SelectedItem = Pictures1[0];
            list2.SelectedItem = Pictures2[0];
        }
    }
}

您還可以綁定更多信息,例如擴展名,大小或任何可以幫助用戶的屬性。

希望這可以幫助...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM