WPF:为每个对象创建相同的网格

时间:2017-03-30 21:19:46

标签: c# wpf binding

我有一个看起来像这样的玩家网格:image 这是代码:

<Grid HorizontalAlignment="Left" VerticalAlignment="Top" Margin="5" Background="AliceBlue">
        <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <Label Grid.Column="4" Grid.Row="0" FontWeight="Bold">Pool</Label>
        <Label Grid.Column="5" Grid.Row="0" FontWeight="Bold">Edge</Label>
        <Label Grid.ColumnSpan="2" Grid.Row="0" HorizontalAlignment="Center" FontWeight="Bold">Felix</Label>
        <Label Grid.Column="0" Grid.Row="1">Might</Label>
        <Button x:Name="btMightP1" Margin="3" Grid.Column="1" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" Padding="3, 0, 3, 0" Click="btMightP1_Click">+</Button>
        <TextBox  x:Name="tbMight1" HorizontalAlignment="Center" VerticalAlignment="Center" Padding="2" Grid.Column="2" Grid.Row="1" MinWidth="40">10</TextBox>
        <Button x:Name="btMightM1" Margin="3" Grid.Column="3" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" Padding="4, 0, 4, 0" Click="btMightM1_Click">-</Button>
        <TextBox Name="lbMightPool1" Grid.Row="1" Grid.Column="4" HorizontalAlignment="Center">0</TextBox>
        <TextBox Name="lbMightEdge1" Grid.Row="1" Grid.Column="5" HorizontalAlignment="Center">0</TextBox>
        <Label Grid.Column="0" Grid.Row="2">Speed</Label>
        <Button x:Name="btSpeedP1" Margin="3" Grid.Column="1" Grid.Row="2" HorizontalAlignment="Center" VerticalAlignment="Center" Padding="3, 0, 3, 0" Click="btSpeedP1_Click">+</Button>
        <TextBox x:Name="tbSpeed1" HorizontalAlignment="Center" VerticalAlignment="Center" Padding="2" Grid.Column="2" Grid.Row="2" MinWidth="40">10</TextBox>
        <Button x:Name="btSpeedM1" Margin="3" Grid.Column="3" Grid.Row="2" HorizontalAlignment="Center" VerticalAlignment="Center" Padding="4, 0, 4, 0" Click="btSpeedM1_Click">-</Button>
        <TextBox Name="lbSpeedPool1" Grid.Row="2" Grid.Column="4" HorizontalAlignment="Center">0</TextBox>
        <TextBox Name="lbSpeedEdge1" Grid.Row="2" Grid.Column="5" HorizontalAlignment="Center">0</TextBox>
        <Label Grid.Column="0" Grid.Row="3">Intellect</Label>
        <Button x:Name="btIntellectP1" Margin="3" Grid.Column="1" Grid.Row="3" HorizontalAlignment="Center" VerticalAlignment="Center" Padding="3, 0, 3, 0" Click="btIntellectP1_Click">+</Button>
        <TextBox x:Name="tbIntellect1" HorizontalAlignment="Center" VerticalAlignment="Center" Padding="2" Grid.Column="2" Grid.Row="3" MinWidth="40">10</TextBox>
        <Button x:Name="btIntellectM1" Margin="3" Grid.Column="3" Grid.Row="3" HorizontalAlignment="Center" VerticalAlignment="Center" Padding="4, 0, 4, 0" Click="btIntellectM1_Click">-</Button>
        <TextBox Name="lbIntellectPool1" Grid.Row="3" Grid.Column="4" HorizontalAlignment="Center">0</TextBox>
        <TextBox Name="lbIntellectEdge1" Grid.Row="3" Grid.Column="5" HorizontalAlignment="Center">0</TextBox>
    </Grid>
播放器类的

代码:

public class Player
{
    public DataClass Might { get; set; }
    public DataClass Speed { get; set; }
    public DataClass Intellect { get; set; }
    public string Name { get; set; }
    public Player()
    {
        Might = new DataClass();
        Speed = new DataClass();
        Intellect = new DataClass();
    }

和DataClass:

public class DataClass
{
    public string Current { get; set; }
    public string Pool { get; set; }
    public string Edge { get; set; }
    public DataClass()
    {
        Current = "0";
        Pool = "0";
        Edge = "0";
    }
}

我的问题是每次创建Player的对象时如何创建相同的网格,并将TextBoxes与新创建的对象的属性绑定?这是否只能使用数据绑定来实现?我试过使用它,但我不太明白绑定是如何工作的。

1 个答案:

答案 0 :(得分:0)

使用listbox或ItemsControl。

<Listbox ItemSource="{Binding YourListOfPlayers}">
    <Listbox.ItemTemplate>
        <DataTemplate>
            <Put your common grid here/>
        </DataTemplate>
    </Listbox.ItemTemplate>
</Listbox>