Item Demon
<navigation:Page x:Class="DataGridAndChart.Home" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation" mc:Ignorable="d" d:DesignWidth="1240" d:DesignHeight="480" Style="{StaticResource PageStyle}" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"> <Grid x:Name="LayoutRoot"> <Grid.Resources> <Popup x:Key="pop" Width="150" Height="75" IsOpen="False" MouseLeave="Popup_MouseLeave"> <Grid > <Grid.RowDefinitions> <RowDefinition Height="23"></RowDefinition> <RowDefinition Height="*"></RowDefinition> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"></ColumnDefinition> <ColumnDefinition Width="20"></ColumnDefinition> </Grid.ColumnDefinitions> <Border Background="Silver" Grid.ColumnSpan="2"></Border> <Button Grid.Column="1" Background="Silver" BorderBrush="Silver" BorderThickness="0" HorizontalAlignment="Right" Width="20" Content="X" x:Name="closePop" Click="closePop_Click"></Button> <StackPanel Grid.Row="1" Grid.ColumnSpan="2"> <Button Content="Display History Sales Info" Background="Silver" BorderBrush="Silver" BorderThickness="0" Height="23"></Button> <Button Content="Go to source" Background="Silver" BorderBrush="Silver" BorderThickness="0" Height="23"></Button> <Button Content="Mark this item" Background="Silver" BorderBrush="Silver" BorderThickness="0" Height="23"></Button> </StackPanel> </Grid> </Popup> </Grid.Resources> <ScrollViewer x:Name="PageScrollViewer" Style="{StaticResource PageScrollViewerStyle}"> <StackPanel x:Name="ContentStackPanel" Style="{StaticResource ContentStackPanelStyle}"> <StackPanel x:Name="toolPanel" Orientation="Horizontal"> <ComboBox SelectionChanged="detailOptions_SelectionChanged" Name="detailOptions" Width="100" DisplayMemberPath="Name" SelectedValuePath="{Binding Value}" ItemsSource="{Binding DetailsOptions}"/> <Button x:Name="feedBackBtn" Width="100" Content="Feed Back" Command="{Binding FeedBackCMD}"></Button> </StackPanel> <sdk:DataGrid Name="dg" IsReadOnly="True" AutoGenerateColumns="False" AlternatingRowBackground="Silver" CanUserReorderColumns="True" CanUserResizeColumns="True" CanUserSortColumns="True" SelectionMode="Extended" SelectedItem="{Binding SelectedItem}" ItemsSource="{Binding DataSource}"> <sdk:DataGrid.Columns> <sdk:DataGridTextColumn Width="250" Header="Item Name" Binding="{Binding ItemName}"></sdk:DataGridTextColumn> <sdk:DataGridTextColumn Width="150" Header="Shop Name" Binding="{Binding ShopName}"></sdk:DataGridTextColumn> <sdk:DataGridTextColumn Width="150" Header="Price Range" Binding="{Binding PriceRange}"></sdk:DataGridTextColumn> <sdk:DataGridTextColumn Width="100" Header="Sold Number" Binding="{Binding SoldNum}"></sdk:DataGridTextColumn> <sdk:DataGridTextColumn Width="150" Header="Sold Amount" Binding="{Binding SoldAmount}"></sdk:DataGridTextColumn> <sdk:DataGridTemplateColumn Header="Check"> <sdk:DataGridTemplateColumn.CellTemplate> <DataTemplate> <Button Content="Go to Source" x:Name="goToSourceBtn" Command="{Binding DataContext.GotToSourceCMD,ElementName=dg}"></Button> </DataTemplate> </sdk:DataGridTemplateColumn.CellTemplate> </sdk:DataGridTemplateColumn> </sdk:DataGrid.Columns> <sdk:DataGrid.RowDetailsTemplate> <!-- Begin row details section. --> <DataTemplate> <Border BorderBrush="Black" BorderThickness="1" Background="Tan" Margin="20,5,0,0"> <sdk:DataGrid AutoGenerateColumns="False" AlternatingRowBackground="Gray" CanUserReorderColumns="True" CanUserResizeColumns="True" CanUserSortColumns="True" SelectionMode="Extended" IsReadOnly="True" ItemsSource="{Binding Details}"> <sdk:DataGrid.Columns> <sdk:DataGridTextColumn Width="380" Header="Item Name" Binding="{Binding ItemName}"></sdk:DataGridTextColumn> <sdk:DataGridTextColumn Width="150" Header="Price Range" Binding="{Binding PriceRange}"></sdk:DataGridTextColumn> <sdk:DataGridTextColumn Width="100" Header="Sold Number" Binding="{Binding SoldNum}"></sdk:DataGridTextColumn> <sdk:DataGridTextColumn Width="150" Header="Sold Amount" Binding="{Binding SoldAmount}"></sdk:DataGridTextColumn> </sdk:DataGrid.Columns> </sdk:DataGrid> </Border> </DataTemplate> <!-- End row details section. --> </sdk:DataGrid.RowDetailsTemplate> </sdk:DataGrid> </StackPanel> </ScrollViewer> </Grid> </navigation:Page>
namespace DataGridAndChart { using System.Windows.Controls; using System.Windows.Navigation; using System.Collections.ObjectModel; using System; using System.Windows.Input; using System.Windows.Media; using System.Windows; using System.Windows.Controls.Primitives; /// <summary> /// Home page for the application. /// </summary> public partial class Home : Page { /// <summary> /// Creates a new <see cref="Home"/> instance. /// </summary> public Home() { InitializeComponent(); this.Title = ApplicationStrings.HomePageTitle; this.DataContext = new HomeViewModel(); dg.LoadingRow += new EventHandler<DataGridRowEventArgs>(dg_LoadingRow); } void dg_LoadingRow(object sender, DataGridRowEventArgs e) { e.Row.MouseRightButtonDown += new MouseButtonEventHandler(Row_MouseRightButtonDown); } void Row_MouseRightButtonDown(object sender, MouseButtonEventArgs e) { DataGridRow row= (DataGridRow)sender; if (row == null) return; Point p = e.GetPosition(Application.Current.RootVisual); Popup pop = LayoutRoot.Resources["pop"] as Popup; GeneralTransform gt = row.TransformToVisual(Application.Current.RootVisual); pop.HorizontalOffset = p.X; pop.VerticalOffset = p.Y; pop.IsOpen = true; e.Handled = true; } private void closePop_Click(object sender, System.Windows.RoutedEventArgs e) { Popup pop = LayoutRoot.Resources["pop"] as Popup; pop.IsOpen = false; } /// <summary> /// Executes when the user navigates to this page. /// </summary> protected override void OnNavigatedTo(NavigationEventArgs e) { } private void detailOptions_SelectionChanged(object sender, SelectionChangedEventArgs e) { ComboBox cb = sender as ComboBox; var item = cb.SelectedValue as NameValue; string option = item == null ? "Selected" : item.Value as string; switch (option) { case "Selected": dg.RowDetailsVisibilityMode = DataGridRowDetailsVisibilityMode.VisibleWhenSelected; break; case "All": dg.RowDetailsVisibilityMode = DataGridRowDetailsVisibilityMode.Visible; break; case "None": dg.RowDetailsVisibilityMode = DataGridRowDetailsVisibilityMode.Collapsed; break; } } private void Popup_MouseLeave(object sender, MouseEventArgs e) { Popup pop = LayoutRoot.Resources["pop"] as Popup; pop.IsOpen = false; } } public class HomeViewModel { public HomeViewModel() { dataSource = ItemSummary.Get(); } ObservableCollection<NameValue> detailOptions = new ObservableCollection<NameValue>(new NameValue[] { new NameValue { Name ="Selected Item(default)",Value ="Selected"}, new NameValue {Name ="All Items",Value ="All"}, new NameValue {Name ="None",Value ="None"} }); public ObservableCollection<NameValue> DetailsOptions{ get { return detailOptions; } } ObservableCollection<ItemSummary> dataSource; public ObservableCollection<ItemSummary> DataSource { get { return dataSource; } } public ItemSummary SelectedItem { get; set; } } public class NameValue { public object Name { get; set; } public object Value { get; set; } } public class ItemSummary { public string ItemName { get; set; } public string ShopName { get; set; } public string PriceRange { get; set; } public double SoldNum { get; set; } public double SoldAmount { get; set; } ObservableCollection<ItemSummary> details = new ObservableCollection<ItemSummary>(); public ObservableCollection<ItemSummary> Details { get { return details; } } public static ObservableCollection<ItemSummary> Get() { Random r = new Random(); ObservableCollection<ItemSummary> list = new ObservableCollection<ItemSummary>(); for (int i = 0; i < 15; i++) { ItemSummary item = new ItemSummary(); item.ItemName = "Sumsung/Cell Phone Glax S " + r.Next(1, 40); item.ShopName = "Shop_" + i; item.PriceRange = (r.NextDouble() + 1) * 1000 + "-" + (r.NextDouble() + 1) * 1000; item.SoldNum = r.Next(1, 100); item.SoldAmount = (r.NextDouble()+0.1)*10000; for (int j = 0; j < r.Next(2, 10); j++) { ItemSummary item2 = new ItemSummary(); item2.ItemName =item .ItemName + " Package " + r.Next(1, 9); item2.PriceRange = ((r.NextDouble() + 1) * 1000).ToString("0.00"); item2.SoldNum = r.Next(1, 100); item2.SoldAmount = ((r.NextDouble() + 0.1) * 10000); item.Details.Add(item2); } list.Add(item); } return list; } } }