如何从 WPF/XAML ListView 读取子项值?

发布于 2024-08-03 07:51:51 字数 870 浏览 6 评论 0原文

够了 - 我刚刚花了一个小时四处搜索,试图找出如何从 ListView 读取 ListViewSubItem 值(如果这是 XAML 中的正确术语)。这是一个小ListView:

<ListView x:Name="CreatableAccounts" ItemsSource="{Binding Processable}" Margin="10,0">
        <ListView.View>
              <GridView>
                    <GridViewColumn Header="Site Name" DisplayMemberBinding="{Binding SiteName}"/>
                    <GridViewColumn Header="From Package" DisplayMemberBinding="{Binding FiCodeDLL.Name}"/>
                </GridView>
         </ListView.View>
</ListView>

这是我尝试读取显然行不通的值:

private void CreateAccounts_Click(object sender, RoutedEventArgs e)
    {
        ListViewItem selected = CreatableAccounts.SelectedItem;
        selected.Ite //  no Items, Text or similar property


    }

任何人都可以指出我正确的方向吗?提前感谢您的帮助!

Enough is enough - I've just spent an hour searching around trying to find out how to read the ListViewSubItem values (if that's correct terminology in XAML) from a ListView. Here's a little ListView:

<ListView x:Name="CreatableAccounts" ItemsSource="{Binding Processable}" Margin="10,0">
        <ListView.View>
              <GridView>
                    <GridViewColumn Header="Site Name" DisplayMemberBinding="{Binding SiteName}"/>
                    <GridViewColumn Header="From Package" DisplayMemberBinding="{Binding FiCodeDLL.Name}"/>
                </GridView>
         </ListView.View>
</ListView>

and here's my attempt to read the values which is clearly not going to work:

private void CreateAccounts_Click(object sender, RoutedEventArgs e)
    {
        ListViewItem selected = CreatableAccounts.SelectedItem;
        selected.Ite //  no Items, Text or similar property


    }

Can anyone point me in the right direction? Grazie in advance for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞陪衬 2024-08-10 07:51:51

ListView 有一个依赖属性“SelectedItem”,您可以将集合子项的实例绑定到该属性,因此:

<DockPanel>
    <Button DockPanel.Dock="Top" Click="Button_Click">Selected Item</Button>
    <ListView ItemsSource="{Binding AllItems}" SelectedItem="{Binding SelectedItem}">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" />
                <GridViewColumn Header="Age" DisplayMemberBinding="{Binding Age}" />
            </GridView>
        </ListView.View>
    </ListView>
</DockPanel>

现在您可以创建一个 ViewModel,它公开填充了您的项目的 ObservableCollection,以及作为您的 SelectedItem 的项目的单个实例...

类似:

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();

        WindowViewModel vm = new WindowViewModel();
        this.DataContext = vm;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        WindowViewModel vm = this.DataContext as WindowViewModel;
        MessageBox.Show(vm.SelectedItem.Name);
    }

}

public class WindowViewModel
{
    public WindowViewModel()
    {
        AllItems = new ObservableCollection<Person>();
        AllItems.Add(new Person { Name = "Joe", Age = 26 });
        AllItems.Add(new Person { Name = "Mary", Age = 23 });
        AllItems.Add(new Person { Name = "Bill", Age = 32 });
        AllItems.Add(new Person { Name = "Harry", Age = 36 });
        AllItems.Add(new Person { Name = "Julie", Age = 18 });
        AllItems.Add(new Person { Name = "Claire", Age = 42 });
    }

    public ObservableCollection<Person> AllItems { get; set; }

    private Person _selectedItem;
    public Person SelectedItem
    {
        get { return _selectedItem; }
        set { _selectedItem = value; }
    }
}

public class Person : INotifyPropertyChanged
{

    private string _name;
    private int _age;

    public string Name
    {
        get { return _name; }
        set
        {
            if (_name != value)
            {
                _name = value;
                RaisePropertyChanged("Name");
            }
        }
    }

    public int Age
    {
        get { return _age; }
        set
        {
            if (_age != value)
            {
                _age = value;
                RaisePropertyChanged("Age");
            }
        }
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    #endregion
}

希望这有帮助:)

Ian

The ListView has a dependency property "SelectedItem" to which you can bind an instance of your collection child item, thus:

<DockPanel>
    <Button DockPanel.Dock="Top" Click="Button_Click">Selected Item</Button>
    <ListView ItemsSource="{Binding AllItems}" SelectedItem="{Binding SelectedItem}">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" />
                <GridViewColumn Header="Age" DisplayMemberBinding="{Binding Age}" />
            </GridView>
        </ListView.View>
    </ListView>
</DockPanel>

Now you can create a ViewModel that exposes an ObservableCollection filled with your items, and a single instance of an item which is your SelectedItem...

something like:

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();

        WindowViewModel vm = new WindowViewModel();
        this.DataContext = vm;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        WindowViewModel vm = this.DataContext as WindowViewModel;
        MessageBox.Show(vm.SelectedItem.Name);
    }

}

public class WindowViewModel
{
    public WindowViewModel()
    {
        AllItems = new ObservableCollection<Person>();
        AllItems.Add(new Person { Name = "Joe", Age = 26 });
        AllItems.Add(new Person { Name = "Mary", Age = 23 });
        AllItems.Add(new Person { Name = "Bill", Age = 32 });
        AllItems.Add(new Person { Name = "Harry", Age = 36 });
        AllItems.Add(new Person { Name = "Julie", Age = 18 });
        AllItems.Add(new Person { Name = "Claire", Age = 42 });
    }

    public ObservableCollection<Person> AllItems { get; set; }

    private Person _selectedItem;
    public Person SelectedItem
    {
        get { return _selectedItem; }
        set { _selectedItem = value; }
    }
}

public class Person : INotifyPropertyChanged
{

    private string _name;
    private int _age;

    public string Name
    {
        get { return _name; }
        set
        {
            if (_name != value)
            {
                _name = value;
                RaisePropertyChanged("Name");
            }
        }
    }

    public int Age
    {
        get { return _age; }
        set
        {
            if (_age != value)
            {
                _age = value;
                RaisePropertyChanged("Age");
            }
        }
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    #endregion
}

Hope this helps :)

Ian

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文