WPF ListView/GridView Binding

我与影子孤独终老i 提交于 2019-12-11 05:32:05

问题


I am attempting to make a simple VS 2017 Extension that is taking a object and displaying it. I have the data coming back and displaying the json in a text box, so I know the data is coming back correctly. But for some reason the gv is just showing the word "id" twice, as their are two records in the dataset. I have tried so many things I'm loosing track. Plus the documentation seems to be all over the place.

I believe there could be at least 2 issues here...

1) XAML the "Bindings"

2) Binding or adding the data to the LV?

Any help with this would be greatly appreciated!

XAML


<UserControl x:Class="DoWork.AllWorkVSControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             Background="{DynamicResource VsBrush.Window}"
             Foreground="{DynamicResource VsBrush.WindowText}"
             mc:Ignorable="d"
             d:DesignHeight="300" d:DesignWidth="300"
             Name="MyToolWindow">
    <Grid>
        <StackPanel Orientation="Vertical">
            <TextBlock Margin="10" HorizontalAlignment="Center">AllWorkVS</TextBlock>
            <Button Content="Click me!" Click="button1_Click" Width="120" Height="80" Name="button1"/>
            <TextBox Height="200" TextWrapping="Wrap" Name="txtJson"/>
            <ListView x:Name="LvIssues">
                <ListView.View>
                    <GridView>
                        <GridView.Columns>
                            <GridViewColumn Width="Auto" Header="Id"  DisplayMemberBinding="{Binding Source='Id'}"></GridViewColumn>
                        </GridView.Columns>
                    </GridView>
                </ListView.View>
            </ListView>
        </StackPanel>
    </Grid>
</UserControl>

C#


public class People
{
    public string Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public partial class AllWorkVSControl : UserControl
{

    public AllWorkVSControl()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        var t = Issues.GetPeopleById("2");
        PopulateListView();
        MessageBox.Show(t);
    }

    private void PopulateListView()
    {
        var l = GetPeople();
        txtJson.Text = JsonConvert.SerializeObject(l);

        foreach (var p in l)
        {
            LvIssues.Items.Add(p);
        }
    }
}

回答1:


you need to set the ListView.ItemsSource.

private void PopulateListView()
{
    var l = GetPeople();
    txtJson.Text = JsonConvert.SerializeObject(l);

    LvIssues.ItemsSource= l;
}

<ListView x:Name="LvIssues">
            <ListView.View>
                <GridView>
                    <GridView.Columns>
                        <GridViewColumn Width="Auto" Header="Id"  DisplayMemberBinding="{Binding Id}"></GridViewColumn>
                    </GridView.Columns>
                </GridView>
            </ListView.View>
        </ListView>


来源:https://stackoverflow.com/questions/47274096/wpf-listview-gridview-binding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!