使用DataTemplates从ListView中的Selecteditem获取样式

时间:2018-06-15 21:39:59

标签: c# wpf

我正在尝试从ListView中收集数据,但是当它来自DataTemplate时收集数据时遇到了问题。

//wpf code (basic)
<ListView x:Name="anotherList">
    <ListViewItem Tag="aTag" Content="A"/>
    <ListViewItem Tag="bTag" Content="B"/>
</ListView>
//c# code
// In order to access data in the ListView I would do this
ListViewItem selectedItem = (ListViewItem)anotherList.SelectedItem;
String selectedContent = selectedItem.Content;

现在当我尝试在其中包含DataTemplate时,我无法使用相同的方法来访问wpf中的数据。

//wpf code using data template
<ListView x:Name="mainList">
    <ListView.ItemsSource>
        // Accessing data from an inline xml table
        <Binding Source="{StaticResource Book1}" XPath="Entry"/> 
    </ListView.ItemsSource>
    <ListView.ItemTemplate>
        <DataTemplate>
            <ListViewItem Tag="{Binding XPath=Tag}" Content="{Binding XPath=LastName}"/>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>
//c# code
//gives null
var selectedItem = (ListViewItem)mainList.SelectedItem //....;
// throws exception
String selectedContent = selectedItem.Content;

不确定如何继续使用c#代码,因为我选择的项目是整个XML内容字符串,无法访问内容或标记。 (即它正确地显示在可视化树中,但ListViewItem的行为与ListViewItem对象不同。)有没有办法更改我的WPF代码,以便我可以像在基本的c#代码中那样从后面访问样式?

非常感谢帮助和感谢阅读。

1 个答案:

答案 0 :(得分:0)

这个基于MVVM的方法适合我

XAML:

process.stdout.write

视图模型:

<Window x:Class="WpfApp1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApp1"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800"
    d:DataContext="{d:DesignInstance local:MyViewModel}">
<Grid>
    <ListView ItemsSource="{Binding Users}" SelectedItem="{Binding SelectedUser}">
        <ListView.ItemTemplate>
            <DataTemplate >
                <Grid d:DataContext="{d:DesignInstance local:User}">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="10"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <TextBlock Text="{Binding FirstName}"/>
                    <TextBlock Text="{Binding LastName}" Grid.Column="2"/>
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid>

DataContext绑定:

public class MyViewModel
{
    private User _selectedUser;

    public MyViewModel()
    {
        Users = new ObservableCollection<User>
        {
            new User{FirstName = "User A FirstName", LastName = "User A LastName"},
            new User{FirstName = "User B FirstName", LastName = "User B LastName"},
            new User{FirstName = "User C FirstName", LastName = "User C LastName"}
        };
    }

    public ObservableCollection<User> Users { get; set; }

    public User SelectedUser
    {
        get { return _selectedUser; }
        set
        {
            _selectedUser = value;
            Debug.WriteLine(_selectedUser.LastName);
        }
    }
}

public class User
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}