-1

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
        {
        }
4

1 に答える 1

0

これは、「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;
        }

    }

コードをコピーして a に貼り付けるだけでFile -> New -> WPF Application、結果を確認できます。

ここでは一般的なソリューション ( Selectable<T>) を使用しているため、必要なクラスでそれを使用できることに注意してください。

this.textBox1.Text = whateverまた、 WPF で次のようなことをしないでください。WPF では UI とデータの分離が推奨されており、WPF を適切に操作するには、 UI はデータではないことを理解する必要があります。WPF に良い結果を期待する場合は、winforms の考え方を忘れてください。

代わりに、適切な ViewModel を作成してテキスト ボックスに表示されるデータを保持するか、SelectedItems私の例のようにテキスト ボックスを のインスタンスに直接バインドします。

余談ですが、トピック外のコメントとして、そのnormal方法はもはやwinformsの方法ではありません。最近の ( < 10 Years) テクノロジ (WPF、Silverlight、WinRT) はすべて XAML ベースであり、MVVM の使用を推奨しています。これは、winforms の方法がold方法ではなく方法であることを意味しnormalます。

于 2013-03-04T15:06:45.037 に答える