列表视图中的 WPF 数据绑定

问题描述 投票:0回答:1

我有以下 ListView,我希望将其绑定到我的视图模型中的自定义列表:

<ListView x:Name="listviewPlayers"  ItemsSource="{Binding Players}" Height="200">
            <ListView.View>
                <GridView>
                    <GridViewColumn Width="235">
                        <GridViewColumn.Header>
                            <Label Grid.Column="2" Content ="Name" Foreground="Black" FontSize="10"></Label>
                        </GridViewColumn.Header>
                    </GridViewColumn>
                    <GridViewColumn Width="235">
                        <GridViewColumn.Header>
                            <Label Grid.Column="1" Content ="Score" Foreground="Black" FontSize="10"></Label>
                        </GridViewColumn.Header>
                    </GridViewColumn>
                    <GridViewColumn Width="235">
                        <GridViewColumn.Header>
                            <Label Grid.Column="0" Content ="Games" Foreground="Black" FontSize="10"></Label>
                        </GridViewColumn.Header>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
            <ListView.ItemTemplate>
                <DataTemplate DataType="local:MainPageViewModel">
                    <Grid Height="25">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition />
                                <ColumnDefinition />
                                <ColumnDefinition />
                            </Grid.ColumnDefinitions>
                            <Label Content="{Binding Name}" FontWeight="Bold" FontSize="10" Foreground="Black">
                            </Label>
                        <Label Grid.Column="1" Content="{Binding CurrentScore}" FontSize="10" HorizontalContentAlignment="Center" Foreground="Black">
                            </Label>
                        <Label Grid.Column="2" Content="{Binding GamesWon}" HorizontalContentAlignment="Center" FontSize="10" Foreground="Black">
                            </Label>
                        </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

在我的视图模型中我有玩家:

public ObservableCollection<Player> Players
    {
        get { return players; }
        set
        {
            players = value;
            foreach (Player p in players)
            {
                PlayerNames.Add(p.Name);
            }
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Players"));
        }


    }

玩家看起来像这样:

public class Player : INotifyPropertyChanged
{

    public event PropertyChangedEventHandler? PropertyChanged;

    private string name = "";

    public string Name
    {
        get { return name; }
        set { name = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Name")); }
    }

    private string currentScore = "0"; 

    public string CurrentScore
    {
        get { return currentScore; }
        set
        {
            currentScore = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("CurrentScore"));
        }
    }

    private string gamesWon = "0";

    public string GamesWon
    {
        get { return gamesWon; }
        set { gamesWon = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("GamesWon")); }
    }

我的 ListView DataTemplate 绑定做错了什么?我已经在主窗口上设置了这个:

d:DataContext="{d:DesignInstance Type=local:MainPageViewModel}"

此时 ListView 看起来像这样:

enter image description here

如有任何建议,我们将不胜感激。

谢谢,

迈克

wpf listview data-binding datatemplate
1个回答
0
投票

您不得设置 ListView 的

ItemTemplate
属性。

改为设置 GridViewColumns 的

DisplayMemberBinding
属性。

<ListView x:Name="listviewPlayers"  ItemsSource="{Binding Players}" Height="200">
    <ListView.View>
        <GridView>
            <GridViewColumn Width="235" Header="Name"
                            DisplayMemberBinding="{Binding Name}"/>
            <GridViewColumn Width="235" Header="Games"
                            DisplayMemberBinding="{Binding CurrentScore}"/>
            <GridViewColumn Width="235" Header="Score"
                            DisplayMemberBinding="{Binding GamesWon}"/>
        </GridView>
    </ListView.View>
</ListView>
© www.soinside.com 2019 - 2024. All rights reserved.