【问题标题】:Listbox SelectedIndices of wpf windowwpf窗口的Listbox SelectedIndices
【发布时间】:2013-03-04 14:40:16
【问题描述】:

如何在 WPF Listbox Window 上使用以下代码。它在正常的 Win 表单 Listbox 上工作正常,所以我想在 WPF 窗口上进行测试,但我收到错误提示

“System.Windows.Controls.ListBox”不包含“SelectedIndices”的定义,并且找不到接受“System.Windows.Controls.ListBox”类型的第一个参数的扩展方法“SelectedIndices”(您是否缺少using 指令还是程序集引用?)

这是我的原始代码

 private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        try
        {

            this.textBox1.Text = people[listBox1.SelectedIndices[0]].name;
            this.connectbox.Text = people[listBox1.SelectedIndices[0]].ipaddress;




        }
        catch
        {
        }

【问题讨论】:

  • It works fine on normal Win form Listbox - 如果您只想从 winforms 复制和粘贴代码,WPF 可能不适合您。 WPF 需要一种完全不同的心态 (MVVM),这与 winform 的传统操作 ui-elements-in-code-behind 心态不同。

标签: c# listbox listboxitems


【解决方案1】:

这是我所说的“WPF 心态”的一个例子,它与将所有逻辑和 UI 混合在一起的古老 winforms 心态有很大不同:

<Window x:Class="WpfApplication4.Window11"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window11" Height="300" Width="300">
    <DockPanel>
        <Button DockPanel.Dock="Top" Content="Copy" Command="{Binding CopyCommand}"/>

        <UniformGrid Columns="2">
            <ListBox ItemsSource="{Binding Items}" SelectionMode="Extended">
                <ListBox.ItemContainerStyle>
                    <Style TargetType="{x:Type ListBoxItem}">
                        <Setter Property="IsSelected" Value="{Binding Mode=TwoWay, Path=IsSelected}"/>
                    </Style>
                </ListBox.ItemContainerStyle>
            </ListBox>
            <ListBox ItemsSource="{Binding SelectedItems}"/>
        </UniformGrid>
    </DockPanel>
</Window>

代码背后:

using System.Linq;
using BaseWPFFramework.MVVM;
using System.Collections.ObjectModel;

namespace WpfApplication4
{
    public partial class Window11
    {
        public Window11()
        {
            InitializeComponent();
            DataContext = new ListViewModel();
        }
    }
}

视图模型:

public class ListViewModel: ViewModelBase
    {
        private ObservableCollection<Selectable<string>> _items;
        public ObservableCollection<Selectable<string>> Items
        {
            get { return _items ?? (_items = new ObservableCollection<Selectable<string>>()); }
        }

        private ObservableCollection<string> _selectedItems;
        public ObservableCollection<string> SelectedItems
        {
            get { return _selectedItems ?? (_selectedItems = new ObservableCollection<string>()); }
        }

        private DelegateCommand _copyCommand;
        public DelegateCommand CopyCommand
        {
            get { return _copyCommand ?? (_copyCommand = new DelegateCommand(Copy)); }
        }

        private void Copy()
        {
            SelectedItems.Clear();
            Items.Where(x => x.IsSelected).Select(x => x.Value).ToList().ForEach(SelectedItems.Add);
        }

        public ListViewModel()
        {
            Enumerable.Range(1, 100).Select(x => new Selectable<string>("Item" + x.ToString())).ToList().ForEach(x => Items.Add(x));
        }
    }

    public class Selectable<T>: ViewModelBase
    {
        private T _value;
        public T Value
        {
            get { return _value; }
            set
            {
                _value = value;
                NotifyPropertyChange(() => Value);
            }
        }

        private bool _isSelected;
        public bool IsSelected
        {
            get { return _isSelected; }
            set
            {
                _isSelected = value;
                NotifyPropertyChange(() => IsSelected);
            }
        }

        public Selectable(T value)
        {
            Value = value;
        }

        public Selectable(T value, bool isSelected): this(value)
        {
            IsSelected = isSelected;
        }

        public override string ToString()
        {
            return Value != null ? Value.ToString() : string.Empty;
        }

    }

只需将我的代码复制并粘贴到 File -&gt; New -&gt; WPF Application 中,然后自己查看结果。

请注意,我使用的是通用解决方案 (Selectable&lt;T&gt;),因此您可以将其用于您想要的任何类。

另外,请不要在 WPF 中执行以下操作:this.textBox1.Text = whatever。 WPF 鼓励 UI 和数据分离,您必须了解 UI Is Not Data 才能正确使用 WPF。如果您期望 WPF 有好的结果,请放弃 winforms 的心态。

相反,要么创建一个适当的 ViewModel 来保存文本框中显示的数据,要么将文本框直接绑定到 SelectedItems 的实例,如我的示例所示。

顺便说一句,题外话,normal 方式不再是 winforms 方式。所有最近的 (&lt; 10 Years) 技术(WPF、Silverlight、WinRT)都是基于 XAML 的,并鼓励使用 MVVM。 这意味着winforms方式现在是old方式,而不是normal方式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-24
    • 1970-01-01
    • 1970-01-01
    • 2018-11-10
    • 1970-01-01
    • 2011-10-19
    • 2015-08-05
    相关资源
    最近更新 更多