Wednesday, January 23, 2013

Windows Phone simple ListBox with click event and showing Dialog

Make a project with the name Decisions and make layout to  MainPage.xaml with following code


<phone:PhoneApplicationPage
    x:Class="Decisions.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">

    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="Application" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="decisions" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>
       
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <ListBox x:Name="Decisions">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Name}" />
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
            <Button Content="Decide For Me" Grid.Row="1" Click="OnDecide"/>
        </Grid>
    </Grid>

</phone:PhoneApplicationPage>

after this make a class with the name DecisionType.cs with the following code


using System;
using System.Collections.Generic;
using System.Linq;

namespace Decisions
{
    public class DecisionType
    {
        public static IEnumerable<DecisionType> GetDecisionTypes()
        {
            return new[]
            {
                new DecisionType
                {
                    Name = "Yes / No",
                    Choices = new[]
                    {
                        "Yes", "No"
                    }
                },
                new DecisionType
                {
                    Name = "A / B / C / D",
                    Choices = new[]
                    {
                        "A", "B", "C", "D"
                    }
                },
                new DecisionType
                {
                    Name = "1 - 100",
                    Choices = Enumerable.Range(1, 100).Select(i => i.ToString())
                },
                new DecisionType
                {
                    Name = "Left / Center / Right",
                    Choices = new[]
                    {
                        "Left", "Center", "Right"
                    }
                },
                new DecisionType
                {
                    Name = "Buy / Sell / Hold",
                    Choices = new[]
                    {
                        "Buy", "Sell", "Hold"
                    }
                },
                new DecisionType
                {
                    Name = "Agree / Disagree",
                    Choices = new[]
                    {
                        "Agree", "Disagree"
                    }
                },
                new DecisionType
                {
                    Name = "Heads / Tails",
                    Choices = new[]
                    {
                        "Heads", "Tails"
                    }
                },
                 new DecisionType
                {
                    Name = "Russian Roulette",
                    Choices = Enumerable.Repeat("Click", 5).Union(new[] { "Bang" })
                },
            };
        }

        public string Name
        {
            get; set;
        }

        public IEnumerable<string> Choices
        {
            get; set;
        }
    }
}


finally MainPage.xaml.cs code is given below

using System;
using System.Linq;
using System.Windows;

namespace Decisions
{
    public partial class MainPage
    {
        private readonly Random random = new Random();

        public MainPage()
        {
            InitializeComponent();

            Loaded += OnLoaded;
        }

        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            Decisions.ItemsSource = DecisionType.GetDecisionTypes();
        }

        private void OnDecide(object sender, RoutedEventArgs e)
        {
            if(Decisions.SelectedItem == null)
                return;

            var decisionType = (DecisionType)Decisions.SelectedItem;

            var decision = decisionType.Choices.ElementAt(random.Next(decisionType.Choices.Count()));

            MessageBox.Show((" Your decision type is "+decisionType.Name + " & decision is " + decision), "Your Random decision is", MessageBoxButton.OK);
        }
    }
}

No comments:

Post a Comment