如何在XAML中访问父控件内的控件

brc7rcf0  于 2023-09-28  发布在  其他
关注(0)|答案(1)|浏览(104)

我有一个类Player,如果我有一个玩家列表,我想让列表中的每个Player都有一张基本上是Grid的卡片,其中包含ImageTextBlock,我该怎么做?
卡是ListViewItem的样式。
这是一个类:

public class Player
    {
        public string firstname;
        public string lastname;
        public string position;
        public int points;
        public int price;
        public Club club;
    }

这里是风格:

<Style TargetType="ListViewItem" x:Key="PlayerCard">
        <Setter Property="Background" Value="White"/>
        <Setter Property="Template">

            <Setter.Value>
                <ControlTemplate TargetType="ListViewItem">
                    <Border BorderThickness="0,0,0,0.5"
                            BorderBrush="#999999"
                            Height="50"
                            Width="280"
                            Margin="8,5">

                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="50"/>
                                <ColumnDefinition/>
                                <ColumnDefinition Width="50"/>
                            </Grid.ColumnDefinitions>

                            <Border Grid.Column="0"
                                    BorderBrush="#05F1FF"
                                    BorderThickness="1"
                                    CornerRadius="1"
                                    ClipToBounds="True">

                                <Image Width="40" Stretch="Uniform"/>

                            </Border>

                            <TextBlock Margin="10"
                                       Height="30"
                                       Grid.Column="1"/>
                            
                        </Grid>

                    </Border>

                </ControlTemplate>
            </Setter.Value>
            
        </Setter>
    </Style>

我也是XAML和Wpf的新手

zxlwwiss

zxlwwiss1#

使用ItemTemplate

<ListView x:Name="listView">
    <ListView.ItemTemplate>
        <DataTemplate>
            <Border BorderThickness="0,0,0,0.5"
                            BorderBrush="#999999"
                            Height="50"
                            Width="280"
                            Margin="8,5">

                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="50"/>
                        <ColumnDefinition/>
                        <ColumnDefinition Width="50"/>
                    </Grid.ColumnDefinitions>

                    <Border Grid.Column="0"
                                    BorderBrush="#05F1FF"
                                    BorderThickness="1"
                                    CornerRadius="1"
                                    ClipToBounds="True">

                        <Image Width="40" Stretch="Uniform"/>

                    </Border>

                    <TextBlock Margin="10"
                                       Height="30"
                                       Grid.Column="1"
                                       Text="{Binding Lastname}"/>

                </Grid>

            </Border>

        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

请注意,为了能够将模板中的元素绑定到Player类,必须将Player类的成员定义为public properties,例如:

public class Player
{
    public string Firstname { get; set; }
    public string Lastname { get; set; }
    public string Position { get; set; }
    public int Points { get; set; } 
    public int Price { get; set; }
    public Club Club { get; set; }
}

最后,不要忘记将ItemsSource属性设置或绑定到List<Player>

List<Player> players = ...;
listView.ItemsSource = players;

相关问题