ListView在给定窗口中只显示一次

本文关键字:一次 显示 窗口 ListView | 更新日期: 2023-09-27 17:50:24

我是c# WPF应用程序开发的新手,目前我编写了我的第一个应用程序,我能够在窗口上显示列表,但只在一个框中。健身盒子工作完美,但我不能让第二个工作。如果我改变顺序,把WeightList放在第一位,然后它工作,ActivityList不显示。下面是代码:

XAML:

<Grid>
    <Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="178" VerticalAlignment="Top" Width="220" Margin="0,3,0,0">
        <Grid>
            <TextBlock TextWrapping="Wrap" TextAlignment="Center" Height="20" VerticalAlignment="Top"><Run Text="FITNESS"/></TextBlock>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="88*"/>
                    <ColumnDefinition Width="13*"/>
                </Grid.ColumnDefinitions>
                <ListView HorizontalAlignment="Left"
      Height="150" Margin="0,26,0,0" VerticalAlignment="Top" Width="218"
      x:Name="ActivityList"  Grid.ColumnSpan="2">
                    <ListView.View>
                        <GridView>
                            <GridViewColumn Header="Type"
                DisplayMemberBinding="{Binding Type}"/>
                            <GridViewColumn Header="Date"
                DisplayMemberBinding="{Binding Date}"/>
                            <GridViewColumn Header="Distance"
                DisplayMemberBinding="{Binding Distance}"/>
                        </GridView>
                    </ListView.View>
                </ListView>
                <Button Margin="8,0,0,156" Grid.Column="1" Width="20" Height="20" Click="Button_Click" Opacity="0.8">
                    <Grid HorizontalAlignment="Center">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="4*"/>
                            <RowDefinition/>
                        </Grid.RowDefinitions>
                        <Image Source="Images/Button_11-512.png" Width="16" Margin="-15,-4,-1,-5" />
                    </Grid>
                </Button>
            </Grid>
        </Grid>
    </Border>
    <Expander Header="NOTES" HorizontalAlignment="Left" Margin="0,181,0,0" VerticalAlignment="Top" Width="220" Height="75">
        <StackPanel Margin="10,4,0,0">
            <TextBox Margin="4" />
        </StackPanel>
    </Expander>
    <Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="178" VerticalAlignment="Top" Width="220" Margin="238,3,0,0">
        <Grid>
            <TextBlock TextWrapping="Wrap" TextAlignment="Center" Height="20" VerticalAlignment="Top"><Run Text="WEIGHT CONTROL"/></TextBlock>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="88*"/>
                    <ColumnDefinition Width="13*"/>
                </Grid.ColumnDefinitions>
                <ListView HorizontalAlignment="Left"
                    Height="150" Margin="0,26,0,0" VerticalAlignment="Top" Width="218"
                    x:Name="WeightList"  Grid.ColumnSpan="2">
                    <ListView.View>
                        <GridView>
                            <GridViewColumn Header="Date"
                                DisplayMemberBinding="{Binding Date}"/>
                            <GridViewColumn Header="Weight"
                                DisplayMemberBinding="{Binding currentWeight}"/>
                        </GridView>
                    </ListView.View>
                </ListView>
                <Button Margin="8,0,0,156" Grid.Column="1" Width="20" Height="20" Opacity="0.8">
                    <Grid HorizontalAlignment="Center">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="4*"/>
                            <RowDefinition/>
                        </Grid.RowDefinitions>
                        <Image Source="Images/Button_11-512.png" Width="16" Margin="-15,-4,-1,-5" />
                    </Grid>
                </Button>
            </Grid>
        </Grid>
    </Border>
    <Expander Header="NOTES" HorizontalAlignment="Left" Margin="238,181,0,0" VerticalAlignment="Top" Width="220" Height="75">
        <StackPanel Margin="10,4,0,0">
            <TextBox Margin="4" />
        </StackPanel>
    </Expander>
</Grid>

MainWindow.cs

public partial class MainWindow : ModernWindow
{
    public static ObservableCollection<Activity> Activities;
    public static ObservableCollection<Weight> WeightControl;
    public MainWindow()
    {
        InitializeComponent();
        Activities = new ObservableCollection<Activity>() {
            new Activity() {Type = "Running", Date = "15.07.2015", Distance = "5km"},
            new Activity() {Type = "Cycling", Date = "17.07.2015", Distance = "120km"},
            new Activity() {Type = "Swimming", Date = "19.07.2015", Distance = "3km"},
                };
        ActivityList.ItemsSource = Activities;
        WeightControl = new ObservableCollection<Weight>() {
            new Weight() {Date = "15.07.2015", currentWeight = 61.2},
            new Weight() {Date = "17.07.2015", currentWeight = 62.1},
            new Weight() {Date = "19.07.2015", currentWeight = 61.9},
                };

图片有问题

ListView在给定窗口中只显示一次

您忘记设置WeightList.ItemsSource。应该这样做:

WeightList.ItemsSource = WeightControl;