ListView显示在给定的Window中只能运行一次

时间:2015-05-22 20:28:29

标签: c# wpf listview

我是c# WPF 应用程序开发的新手,目前我正在编写第一个应用程序,我能够在一个窗口中显示一个列表,但只能在一个框中显示。 Fitness 框效果很好但我无法获得第二个工作。如果我更改了订单并将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},
                };

Image with problem

1 个答案:

答案 0 :(得分:1)

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

static factory method