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

时间:2009-08-31 06:45:54

标签: wpf xaml listview listviewitem

足够了 - 我只花了一个小时的时间来寻找如何从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


    }

有人能指出我正确的方向吗? Grazie提前帮助你!

1 个答案:

答案 0 :(得分:3)

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
}

希望这会有所帮助:)

伊恩