locked
How to get ListView items to appear

    Question

  • I'm having trouble getting a list view to appear. The header appears, and I can get my app to compile and run, but the items, which should have a red highlight, don't appear when I click the buttons. These buttons respond, but I don't know if the code for adding to the List<> and the ListView is deficient, or if the binding between the two is deficient.

    The List<>, in GamePlay.xaml.cs, is called commands and stores a list of type Command (which has subclasses representing different type of commands) The ListView is called CommandList.

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.InteropServices.WindowsRuntime;
    using Windows.Foundation;
    using Windows.Foundation.Collections;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Controls.Primitives;
    using Windows.UI.Xaml.Data;
    using Windows.UI.Xaml.Input;
    using Windows.UI.Xaml.Media;
    using Windows.UI.Xaml.Navigation;
    using Windows.UI.Popups;
    
    
    
    // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
    
    namespace War_On_Flerbithica
    {
        /// <summary>
        /// An empty page that can be used on its own or navigated to within a Frame.
        /// </summary>
        public sealed partial class GamePlay : Page
        {
            
            public GamePlay()
            {
                this.InitializeComponent();
    
                CommandsInTurn.Source = commands;
                CommandList.SelectionChanged += CommandView_SelectionChanged;
            }
    
            private void CommandView_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
    
            }
    
            private void Zoom_In(object sender, RoutedEventArgs e)
            {
                this.ScrollMap.ChangeView(ScrollMap.HorizontalOffset, ScrollMap.VerticalOffset, ScrollMap.ZoomFactor + 1);
            }
    
            private void Zoom_Out(object sender, RoutedEventArgs e)
            {
                this.ScrollMap.ChangeView(ScrollMap.HorizontalOffset, ScrollMap.VerticalOffset, ScrollMap.ZoomFactor - 1);
            }
    
            private void return_main(object sender, RoutedEventArgs e)
            {
                this.Frame.Navigate(typeof(MainPage));
            }
    
            private void ConfirmCommands(object sender, RoutedEventArgs e)
            {
                
                this.ResultsBox.Text = "Conformed for time" + counter++;
                Boolean allClear = true;
                foreach(Command order in commands)
                {
                    if (!order.checkCommand())
                        allClear = false;
                }
                allClear = false; // TO-DO: delete this statement
                if(allClear)
                {
                    // TO-DO: send commands to currently nonexistant 
                }
                else
                {
                    var messageDialog = new MessageDialog("At least one of your commands is not fully functional! Please fix this issue and try again!");
                    //messageDialog.Commands.Add()
                }
            }
            private int counter = 1; // DEBUG variable
            private List<Command> commands = new List<Command>();
            private Command currentCommand;
            private Location selectedLocation;
            private MilitaryTroop selectedTroop;
            private MilitaryTroop Afterma; // special pointer for the Afterma
    
            private void StartBuildBase(object sender, PointerRoutedEventArgs e)
            {
                commands.Add(new BiuldBaseCommand());
                this.ResultsBox.Text = "Building Army";
                CommandList.UpdateLayout();
            }
    
            private void StartLaunchNavy(object sender, PointerRoutedEventArgs e)
            {
                commands.Add(new LaunchNavyCommand());
                CommandList.UpdateLayout();
            }
    
            private void StartLaunchNavy(object sender, TappedRoutedEventArgs e)
            {
                commands.Add(new LaunchNavyCommand());
                CommandList.UpdateLayout();
            }
    
            private void StartBuildBase(object sender, TappedRoutedEventArgs e)
            {
                commands.Add(new LaunchNavyCommand());
                this.ResultsBox.Text = "Building Army";
                CommandList.UpdateLayout();
            }
    
            private void SelectLocation(object sender, PointerRoutedEventArgs e)
            {
                object loc = e.OriginalSource;
                try 
                {
                    selectedLocation = (Location)loc; // loc should be a Location, though a glitch might cause otherwise
                    if(currentCommand != null)
                    {
                        currentCommand.InvolveLoaction(selectedLocation);
                    }
                }
                catch
                {
    
                }
            }
    
            private void SelectLocation(object sender, TappedRoutedEventArgs e)
            {
                object loc = e.OriginalSource;
                try
                {
                    selectedLocation = (Location)loc;// loc should be a Location, though a glitch might cause otherwise
                    if (currentCommand != null)
                    {
                        currentCommand.InvolveLoaction(selectedLocation);
                    }
                }
                catch
                {
    
                }
            }
    
    
            private void SelectTroop(object sender, TappedRoutedEventArgs e)
            {
                
            }
    
            private void SelectTroop(object sender, PointerRoutedEventArgs e)
            {
    
            }
    
            private void StartRaiseArmy(object sender, PointerRoutedEventArgs e)
            {
                commands.Add(new RaiseArmyCommand());
                this.ResultsBox.Text = "RaiseArmyPoint";
                CommandList.UpdateLayout();
            }
    
            private void StartRaiseArmy(object sender, TappedRoutedEventArgs e)
            {
                commands.Add(new RaiseArmyCommand());
                this.ResultsBox.Text = "RaiseArmyTap";
                CommandList.UpdateLayout();
            }
    
            private void StartTrainArmy(object sender, PointerRoutedEventArgs e)
            {
                commands.Add(new TrainArmyCommand());
                this.ResultsBox.Text = "TrainArmy";
                CommandList.UpdateLayout();
            }
    
            private void StartTrainArmy(object sender, TappedRoutedEventArgs e)
            {
                commands.Add(new TrainArmyCommand());
                this.ResultsBox.Text = "TrainArmy";
                CommandList.UpdateLayout();
            }
    
            private void StartMoveArmy(object sender, PointerRoutedEventArgs e)
            {
                commands.Add(new MoveArmyCommand());
                this.ResultsBox.Text = "Move Army";
                CommandList.UpdateLayout();
            }
    
            private void StartMoveArmy(object sender, TappedRoutedEventArgs e)
            {
                commands.Add(new MoveArmyCommand());
                this.ResultsBox.Text = "Move Army";
                CommandList.UpdateLayout();
            }
    
            private void StartAirAttack(object sender, PointerRoutedEventArgs e)
            {
                commands.Add(new MoveArmyCommand());
                this.ResultsBox.Text = "AirAttack";
                CommandList.UpdateLayout();
            }
            private void StartAirAttack(object sender, TappedRoutedEventArgs e)
            {
                this.ResultsBox.Text = "AirAttack";
                CommandList.UpdateLayout();
            }
    
            private void StartMoveAfterma(object sender, PointerRoutedEventArgs e)
            {
                this.ResultsBox.Text = "AftermaAttack";
            }
    
            private void StartMoveAfterma(object sender, TappedRoutedEventArgs e)
            {
                this.ResultsBox.Text = "Afterma";
            }
    
        }
    }
    The following is the xaml used. I omitted a ton of code regarding a User Control I designed, so it would not be distracting.
    <Page
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:War_On_Flerbithica"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        
        xmlns:UI="using:Microsoft.Advertising.WinRT.UI"
        x:Class="War_On_Flerbithica.GamePlay"
        mc:Ignorable="d">
    
        
        <Page.Resources>
            <CollectionViewSource x:Name="CommandsInTurn" IsSourceGrouped="True"/>
            <!--DataTemplate x:Key="CommandDisplay">
                <Grid>
                    <StackPanel Orientation="Horizontal" Height="60" Width="120">
                        <TextBlock Text="{Binding thisType}" FontSize="20" FontWeight="Bold">
    
                        </TextBlock>
                        <Image>
    
                        </Image>
                    </StackPanel>
                </Grid>
            </-->
            <DataTemplate x:Key="TroopDisplay">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    
                </Grid>
                
            </DataTemplate>
        </Page.Resources>
    
    
        <Grid>
            <Grid.Background>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="Black" Offset="0"/>
                    <GradientStop Color="#FF1CC2F3" Offset="1"/>
                </LinearGradientBrush>
            </Grid.Background>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="170*"/>
                <ColumnDefinition Width="344*"/>
                <ColumnDefinition Width="169*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="89*"/>
                <RowDefinition Height="39*"/>
            </Grid.RowDefinitions>
    
    
    
            <StackPanel HorizontalAlignment="Left" Height="514" Margin="10,10,0,0" VerticalAlignment="Top" Width="320">
                <TextBlock TextWrapping="Wrap" Text="Create Commands" Height="41" FontSize="36"/>
                <ListBox Height="298">
                    <ListBoxItem Content="Build Base" PointerPressed="StartBuildBase" Tapped="StartBuildBase"/>
                    <ListBoxItem Content="Launch Navy" PointerPressed="StartLaunchNavy" Tapped="StartLaunchNavy"/>
                    <ListBoxItem Content="Raise Army" PointerPressed="StartRaiseArmy" Tapped="StartRaiseArmy"/>
                    <ListBoxItem Content="Train Army" PointerPressed="StartTrainArmy" Tapped="StartTrainArmy"/>
                    <ListBoxItem Content="Move Army" PointerPressed ="StartMoveArmy" Tapped="StartMoveArmy"/>
                    <ListBoxItem Content="Air Attack" PointerPressed="StartAirAttack" Tapped="StartAirAttack"/>
                    <ListBoxItem Content="Move Afterma" PointerPressed="StartMoveAfterma" Tapped="StartMoveAfterma"/>
                </ListBox>
                <ComboBox Height="58">
                    <ComboBoxItem Content="(Not Selected)" IsSelected="True"/>
                    <ComboBoxItem Content="Infantry"/>
                    <ComboBoxItem Content="Navy"/>
                    <ComboBoxItem Content="Fighter Jet"/>
                    <ComboBoxItem Content="Bomber"/>
                    <ComboBoxItem Content="Spy"/>
                    <ComboBoxItem Content="Anti-Spy" />
                </ComboBox>
                <StackPanel Height="118" Orientation="Horizontal">
                    <Button Content="Zoom In" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="161" Click="Zoom_In" />
                    <Button Content="Zoom Out" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="159" Click="Zoom_Out" />
                </StackPanel>
    
            </StackPanel>
            <StackPanel HorizontalAlignment="Left" Height="514" VerticalAlignment="Top" Width="668" Grid.Column="1" Margin="10,10,0,0">
                <ScrollViewer x:Name="ScrollMap" MinZoomFactor="1" HorizontalScrollBarVisibility="Visible">
                    <Grid Height="388">
                        <Grid.Background>
                            <ImageBrush Stretch="Fill" ImageSource="world_base.png"/>
                        </Grid.Background>
                        
    
    
                    </Grid>
                </ScrollViewer>
                <GridView Height="129"/>
    
    
    
            </StackPanel>
            <ListView x:Name="CommandList" Grid.Column="2" HorizontalAlignment="Left" Height="524" Margin="10,10,0,0"
                      VerticalAlignment="Top" Width="328" Header="Commands" FontSize="36" FontWeight="Bold"
                      ItemsSource="{Binding Source={StaticResource CommandsInTurn}}">
                      
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Height="20" Width="300" Background="#FFFB6363" Opacity="0.5">
                            <TextBox x:Name="commandType" Text="{Binding thisType}" Width="100"/>
                            <TextBox x:Name="commandDetail"/>
                        </StackPanel>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
            <StackPanel HorizontalAlignment="Left" Height="234" Grid.Row="1" VerticalAlignment="Top" Width="340" Orientation="Horizontal">
                <HyperlinkButton Content="Main Menu" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="168" Click="return_main"/>
                <Button Content="Confirm" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="173" Click="ConfirmCommands"/>
            </StackPanel>
            <StackPanel Grid.Column="1" HorizontalAlignment="Left" Height="234" Grid.Row="1" VerticalAlignment="Top" Width="688" Orientation="Horizontal">
                <TextBox x:Name="ResultsBox" TextWrapping="Wrap" Text="TextBox" Width="487"/>
                
    
            </StackPanel>
    
    
        </Grid>
    </Page>
    I tried to get the items to appear when ever certain buttons, like "Build Base" was clicked. Would anyone know why they don't?


    • Edited by Trec Apps Saturday, December 20, 2014 12:34 AM
    Saturday, December 20, 2014 12:34 AM

Answers

  • The problem is that your CollectionViewSource is marked IsSourceGrouped=true but doesn't contain groups. IsSourceGrouped requires a collection of collections.

    If you change the Xaml as follows then it works fine:

        <Page.Resources>
            <CollectionViewSource x:Name="CommandsInTurn" IsSourceGrouped="False"/>
        </Page.Resources>

    • Marked as answer by Trec Apps Tuesday, December 23, 2014 4:07 AM
    Tuesday, December 23, 2014 3:17 AM
    Owner

All replies

  • List<Command> doesn't fire change notifications so the data binding doesn't know that you added or removed items. If you change this to an ObservableCollection<Command> then it will fire change notifications and the binding will update.

    You should also remove the UpdateLayout calls. They are unnecessary and inefficient.

    Saturday, December 20, 2014 1:23 AM
    Owner
  • Are you sure ObservableCollection is the Class I want to use. I tried it, but I ended up with a "System.Exception", which means I ended up with a lower level error. Looking at the class documentation, it appears that it is only supported on Windows Phone, not Windows 8.1, which I was testing on. Given that the Add(...) method is what triggered the exception, and the fact that the documentation does not tell of exceptions, I think the issue might be using what turned out to be a Windows Phone exclusive class on Windows 8.1.

    Here is the link: http://msdn.microsoft.com/en-us/library/windows/apps/ms668604%28v=vs.105%29.aspx

    Saturday, December 20, 2014 1:44 AM
  • Yes. I use it in almost every app. You linked to the Windows Phone Silverlight specific documentation. The full docs are at http://msdn.microsoft.com/en-us/library/windows/apps/ms668604(v=vs.110).aspx . You can get there by dropping down the "other versions" box.

    You'll need to provide more information if you need help with the error. What exactly does the code look like where it occurs? Does it crash directly in the Add? Or is it when responding to the Add? Rather than posting several pages of code into the forum please post just the relevant lines here and share a minimal repro project that demonstrates the problem on your OneDrive.

    --Rob

    Saturday, December 20, 2014 2:54 AM
    Owner
  • Here are a couple images to show you what happens. Highlighted on the image of the app is the button I click.

    Saturday, December 20, 2014 2:47 PM
  • There isn't enough information to know what is going on. Can you share a minimal repro sample that demonstrates the problem on your OneDrive?
    Monday, December 22, 2014 9:28 PM
    Owner
  • The following link should get you to a zip file that contains a new, smaller version of the project. Interestingly enough, the Add method alone works fine, but when the ObservableCollection is assigned to the CollectionViewSource's source (which I added in the constructor), the Add method becomes glitchy.

    https://onedrive.live.com/redir?resid=8FA45ED2C68B4551!2733&authkey=!AAFqGMT3Kv4_8co&ithint=file%2czip

    Monday, December 22, 2014 10:32 PM
  • The problem is that your CollectionViewSource is marked IsSourceGrouped=true but doesn't contain groups. IsSourceGrouped requires a collection of collections.

    If you change the Xaml as follows then it works fine:

        <Page.Resources>
            <CollectionViewSource x:Name="CommandsInTurn" IsSourceGrouped="False"/>
        </Page.Resources>

    • Marked as answer by Trec Apps Tuesday, December 23, 2014 4:07 AM
    Tuesday, December 23, 2014 3:17 AM
    Owner