How sum the listbox items - c#

I have a listbox with three values. I need get the FEstado value for all items of ListBox and sum them. However, this ListBox not have defined items, the user select items from another ListBox.
My Listbox:
<ListBox Name="List2" HorizontalContentAlignment="Stretch" Grid.ColumnSpan="3" Margin="0,43,-66,0">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17" Width="432" Height="95" Hold="holdListRmv">
<TextBlock Grid.Column="0" Text="{Binding FNome}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
<TextBlock Grid.Column="1" Text="{Binding FEstado}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
<TextBlock Grid.Column="2" Text="{Binding Quantity}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextNormalStyle}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

Assuming that you populate the ListBox by adding model item to ListBox's Items property (as shown in your previous question), then you can get all items from the same ListBox.Items property. And assuming that FEstado is a number, you can do something like this :
var items = List2.Items.Cast<Fields>();
var total = items.Sum(o => o.FEstado);

You can do something like this,
try this:
XAML:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="0">
<StackPanel>
<ListBox Name="List2" HorizontalContentAlignment="Stretch" Grid.ColumnSpan="3" Margin="0,43,-66,0">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17" Width="432" Height="95" >
<TextBlock Grid.Column="0" Text="{Binding FNome}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
<TextBlock Grid.Column="1" Text="{Binding FEstado}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
<TextBlock Grid.Column="2" Text="{Binding Quantity}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextNormalStyle}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Content="sum" Click="Button_Click_1"></Button>
</StackPanel>
</Grid>
cs:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
ObservableCollection<DataClass> obj = new ObservableCollection<DataClass>();
obj.Add(new DataClass("AA", "10", "10"));
obj.Add(new DataClass("BB", "10", "10"));
List2.ItemsSource = obj;
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
int sum = 0;
for (int i = 0; i < List2.Items.Count; i++)
{
DataClass obj = (DataClass)List2.Items[i];
sum += int.Parse(obj.FEstado.ToString());
}
MessageBox.Show(sum.ToString());
}
public class DataClass
{
public string FNome { get; set; }
public string FEstado { get; set; }
public string Quantity { get; set; }
public DataClass() { }
public DataClass(string FNome, string FEstado, string Quantity)
{
this.FNome = FNome;
this.FEstado = FEstado;
this.Quantity = Quantity;
}
}

Related

How to add a new item in listbox which binding from code behind to xaml page

ProjectInformation instance = lstbxindex.SelectedItem as ProjectInformation;
string name = instance.ProjectRow.Name;
IEditableCollectionView items = lstbxindex.Items;
if(items.CanRemove)
{
items.Remove(lstbxindex.SelectedItem);
}
Using these lines of code remove listboxitems. after edit the values I need to add the values in listbox.
XAML
<ListBox ItemsSource="{Binding}" HorizontalContentAlignment="Left" x:Name="lstbxindex" SelectionMode="Extended" Foreground="White" FontSize="20px" Height="241" BorderBrush="#555555" Margin="10,34,16,0" VerticalAlignment="Top" Width="322" Background="#555555" >
<ListBox.ItemTemplate>
<DataTemplate>
<WrapPanel Orientation="Horizontal" Margin="5" >
<TextBlock Height="40px" Width="80px" Text="{Binding Roundedhour1}" FontSize="24" Background="#555555" Foreground="Black"></TextBlock>
<Label x:Name="items" Content="{Binding ProjectRow.Name}" Margin="35,0,0,0" MouseDoubleClick="items_MouseDoubleClick" Foreground="White"></Label>
</WrapPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Set the ItemsSource property of the ListBox to an ObservableCollection<ProjectInformation> and add and remove items from this Collection using the Add and Remove methods.
XAML:
<ListBox HorizontalContentAlignment="Left" x:Name="lstbxindex" SelectionMode="Extended" Foreground="White" FontSize="20px" Height="241" BorderBrush="#555555" Margin="10,34,16,0" VerticalAlignment="Top" Width="322" Background="#555555" >
<ListBox.ItemTemplate>
<DataTemplate>
<WrapPanel Orientation="Horizontal" Margin="5" >
<TextBlock Height="40px" Width="80px" Text="{Binding Roundedhour1}" FontSize="24" Background="#555555" Foreground="Black"></TextBlock>
<Label x:Name="items" Content="{Binding ProjectRow.Name}" Margin="35,0,0,0" MouseDoubleClick="items_MouseDoubleClick" Foreground="White"></Label>
</WrapPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Code:
public class ProjectInformation
{
public int Roundedhour1 { get; set; }
}
public partial class MainWindow : Window
{
private ObservableCollection<ProjectInformation> _sourceCollection = new ObservableCollection<ProjectInformation>();
public MainWindow()
{
InitializeComponent();
lstbxindex.ItemsSource = _sourceCollection;
//add
ProjectInformation item = new ProjectInformation() { Roundedhour1 = 1 };
_sourceCollection.Add(item);
}
private void items_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
//remove
_sourceCollection.Remove(lstbxindex.SelectedItem as ProjectInformation);
}
}

Values from database not showing up on xaml page after using groupby statement

I have a dto,method and xaml file below. The method returns duplicates which is fine. I only want to display one on my xaml page. I used a groupby and I was able
to display one item but the values for RouteNbr and Day are not displayed on the xaml page.
Do you know what am doing wrong?
public class CSDto
{
public Int32 ID { get; set; }
public Int32 RouteNbr { get; set; }
public Int32 Day { get; set; }
public String Month { get; set; }
public String Year { get; set; }
}
private void GetAllCsrActPlansByCsrId(Int32 CssId)
{
try
{
string getCs = Convert.ToString(CssId);
var getRouteInfoProfile = MySol.GetByCustomerNbr(getCs);
var queryResult = getRouteInfoProfile;
grdVwRouteAct.DataContext = queryResult.GroupBy(g=>g.RouteNbr).Distinct();
}
catch (Exception ex)
{
customutility.ApplicationErrorlog(UserID, ex.Message, DateTime.Now.ToString());
}
}
<GridView.ItemTemplate>
<DataTemplate>
<Button Style="{StaticResource CSRProfile}" Margin="0" Padding="0" Height="Auto" Width="Auto" Background="MediumAquamarine">
<StackPanel Tag="cpActionPlanBlock" Width="275" Height="90" HorizontalAlignment="Left" VerticalAlignment="Center" Orientation="Horizontal" Margin="0">
<Image Source="/Assets/Images/CSRProfile-checkicon.gif" Width="49" Height="65" VerticalAlignment="Center" Margin="10"/>
<StackPanel Orientation="Vertical" VerticalAlignment="Center" Margin="10,0">
<StackPanel Orientation="Horizontal">
<TextBlock Tag="cpRouteNumber" Style="{ThemeResource BasicFontStyle4B}" Text="{Binding RouteNbr}" VerticalAlignment="Top" Margin="0,0,0,5"/>
<TextBlock Style="{ThemeResource BasicFontStyle4B}" Text="-" VerticalAlignment="Top" Margin="5,0,5,0"/>
<TextBlock Tag="cpDay" x:Uid="queueItemType" Style="{ThemeResource BasicFontStyle4B}" Text="{Binding Day}" VerticalAlignment="Top" Margin="0,0,0,5"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Tag="cpNumberofActions" Style="{ThemeResource BasicFontStyle4B}" Text="[Y]" VerticalAlignment="Top" Margin="0,0,5,0"/>
<TextBlock Tag="cpLblActions" Style="{ThemeResource BasicFontStyle4B}" Text="Actions" x:Uid="cpActions" VerticalAlignment="Top" Margin="0,0,15,5"/>
</StackPanel>
</StackPanel>
</StackPanel>
</Button>
</DataTemplate>
</GridView.ItemTemplate>

How to bind Accelerometer values to Telerik Linear Guage?

I'm using Telerik Linear Guage to show the Accelerometer. I have three Gauge for X,Y,Z coordinates respectively.
The problem is with the value binding. I used the following code to bind the values, but it is not working,
//CodeBehind
public string XCord { get; set; }
public string YCord { get; set; }
public string ZCord { get; set; }
// Constructor
public MainPage()
{
InitializeComponent();
acc = new Accelerometer();
acc.CurrentValueChanged += acc_CurrentValueChanged;
acc.Start();
}
void acc_CurrentValueChanged(object sender, SensorReadingEventArgs<AccelerometerReading> e)
{
XCord = Math.Abs(acc.CurrentValue.Acceleration.X).ToString();
YCord = Math.Abs(acc.CurrentValue.Acceleration.Y).ToString();
ZCord = Math.Abs(acc.CurrentValue.Acceleration.Z).ToString();
}
<!--XAML CODE-->
<gauges:MarkerGaugeIndicator Value="57.67"
gauges:LinearGaugeRange.IndicatorOffset="40"
x:Name="indicator5"
IsAnimated="True"
IsMarkerRotated="False">
<gauges:MarkerGaugeIndicator.MarkerTemplate>
<DataTemplate>
<Grid Width="37"
Height="37">
<TextBlock x:Name="txt1" Text="{Binding XCord}"
VerticalAlignment="Center"
HorizontalAlignment="Center"
FontSize="13"
FontWeight="Bold"/>
</Grid>
</DataTemplate>
</gauges:MarkerGaugeIndicator.MarkerTemplate>
</gauges:MarkerGaugeIndicator>
<gauges:MarkerGaugeIndicator Value="57.67"
gauges:LinearGaugeRange.IndicatorOffset="40"
x:Name="indicator6"
IsAnimated="True"
IsMarkerRotated="False">
<gauges:MarkerGaugeIndicator.MarkerTemplate>
<DataTemplate>
<Grid Width="37"
Height="37">
<TextBlock x:Name="txt2" Text="{Binding YCord}"
VerticalAlignment="Center"
HorizontalAlignment="Center"
FontSize="13"
FontWeight="Bold"/>
</Grid>
</DataTemplate>
</gauges:MarkerGaugeIndicator.MarkerTemplate>
</gauges:MarkerGaugeIndicator>
<gauges:MarkerGaugeIndicator Value="57.67"
gauges:LinearGaugeRange.IndicatorOffset="40"
x:Name="indicator7"
IsAnimated="True"
IsMarkerRotated="False">
<gauges:MarkerGaugeIndicator.MarkerTemplate>
<DataTemplate>
<Grid Width="37"
Height="37">
<TextBlock x:Name="txt3" Text="{Binding ZCord}"
VerticalAlignment="Center"
HorizontalAlignment="Center"
FontSize="13"
FontWeight="Bold"/>
</Grid>
</DataTemplate>
</gauges:MarkerGaugeIndicator.MarkerTemplate>
</gauges:MarkerGaugeIndicator>
Somebody say me how to bind the value of the coordinates with the control.

Nested Listbox wont display nested list

<ListBox Height="434" HorizontalAlignment="Left" Margin="6,238,0,0" Name="listBox1" VerticalAlignment="Top" Width="432" DataContext="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name}" Margin="4" Foreground="{StaticResource PhoneAccentBrush}"></TextBlock>
<StackPanel Orientation="Horizontal" Margin="4">
<TextBlock Text="Set" Margin="16" Foreground="{StaticResource PhoneAccentBrush}" />
<TextBlock Text="Weight" Margin="16" Foreground="{StaticResource PhoneAccentBrush}" />
<TextBlock Text="Reps" Margin="10,16,0,16" Foreground="{StaticResource PhoneAccentBrush}" />
</StackPanel>
<ListBox Name="setsAndReps" Height="auto" Width="auto" ItemsSource="{Binding Sets}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBox Text="{Binding SetNumber}"/>
<TextBox Text="{Binding Weight}"/>
<TextBox Text="{Binding Reps}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
The outer listbox's item source is being set to an observable collection of a user defined class called excercise
public class excercise : IComparable, IEquatable<excercise>, INotifyPropertyChanged
{
string name;
int max;
int NUM_SETS;
ObservableCollection<set> sets;
public event PropertyChangedEventHandler PropertyChanged;
public string Name
{
get { return this.name; }
set { this.name = value; }
}
public excercise(string name)
{
this.name = name;
this.NUM_SETS = 0;
this.sets = new ObservableCollection<set>();
}
public ObservableCollection<set> Sets
{
get{return this.sets; }
}
public ObservableCollection<set> getSets()
{
return this.sets;
}
}
The properties in the inner list box are from the set class but none of them are being displayed and I am not sure what the problem is.
your first listbox doesn't have any itemsource set and the datacontext is set to an empty binding (both on line 1 of your code)

Selecting multiple entries from XML with same name WP7

I have tried for several hours and searching on the net for examples on how to pull multiple elements with the same name however different attributes and binding those to my XAML in my app for wp7,
the easiest way for me to explain is to simply show you,
heres what I have so far
public class Match
{
public string HomeTeam { get; set; }
public string AwayTeam { get; set; }
public string HomeScore { get; set; }
public string AwayScore { get; set; }
public string GoalsPlayer { get; set; }
public string goal { get; set; }
public string GoalsTime { get; set; }
public string DismissalsPlayer { get; set; }
public string DismissalsTime { get; set; }
public string BookingPlayer { get; set; }
public string BookingTime { get; set; }
public string GameTime { get; set; }
}
void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
XElement element = XElement.Parse(e.Result);
try
{
listBox2.ItemsSource = from item in element.Descendants("competition")
from match in item.Elements("match").Where(arg => arg.Attribute("awayTeam").Value == team | arg.Attribute("homeTeam").Value == team)
select new Match
{
HomeTeam = (string)match.Attribute("homeTeam"),
AwayTeam = (string)match.Attribute("awayTeam"),
HomeScore = (string)match.Attribute("homeTeamScore"),
AwayScore = (string)match.Attribute("awayTeamScore"),
GoalsPlayer = (string)match.Attribute("playerName"),
GoalsTime = (string)match.Attribute("time"),
};
}
catch (Exception ex)
{
Debug.WriteLine(ex.StackTrace);
}
here is my XAML
<ListBox Name="listBox2" Grid.Row="0">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Name="teams" Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<TextBlock Margin="0,10,0,0" Foreground="White" HorizontalAlignment="Left" Text="{Binding HomeTeam}"/>
<TextBlock Margin="10,10,30,0" Foreground="White" Text="{Binding HomeScore}" TextWrapping="Wrap" FontSize="20" />
<TextBlock Margin="0,10,0,0" Foreground="White" HorizontalAlignment="Left" Text="{Binding AwayTeam}"/>
<TextBlock Margin="10,10,30,0" Foreground="White" Text="{Binding AwayScore}" TextWrapping="Wrap" FontSize="20" />
</StackPanel>
<StackPanel Name="scores" Orientation="Horizontal">
<TextBlock Margin="0,10,0,0" Foreground="White" Text="{Binding GoalsPlayer}" TextWrapping="Wrap" FontSize="20" />
<TextBlock Margin="10,10,30,0" Foreground="White" Text="{Binding GoalsTime}" TextWrapping="Wrap" FontSize="20" />
</StackPanel>
<StackPanel Name="dismissals">
<TextBlock Foreground="White" Text="{Binding DismissalsPlayer}" TextWrapping="Wrap" FontSize="20"/>
<TextBlock Foreground="White" Text="{Binding DismissalsTime}" TextWrapping="Wrap" FontSize="20"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
here is my XML
<match homeTeam="Arsenal" awayTeam="Liverpool" homeTeamScore="0" awayTeamScore="2">
<goal time="77:13" playerName="Aaron Ramsey" />
<goal time="89:57" playerName="Luis Suarez"/>
<dismissal time="69:59" playerName="Emmanuel Frimpong"/>
</match>
I understand that I will only get one entry on my Page for Goals, which would be the first goal element in my XML
how can I go about that and implement every
<goal ...>
<goal ...>
into my Datatemplate that currently only shows the first entry, another potential problem is I cannot guarantee how many goals there will be so I am a really unsure on how to go about it entirely
thanks
John
Should playerName even resolve on your match? Because that's where you are resolving it in your XML query.
Regardless, what you are looking for here is a sub-object that can be held in a list property of the Match object:
listBox2.ItemsSource = from item in element.Descendants("competition")
from match in item.Elements("match")
.Where(arg => arg.Attribute("awayTeam").Value == team ||
arg.Attribute("homeTeam").Value == team)
select new Match
{
HomeTeam = (string)match.Attribute("homeTeam"),
AwayTeam = (string)match.Attribute("awayTeam"),
HomeScore = (string)match.Attribute("homeTeamScore"),
AwayScore = (string)match.Attribute("awayTeamScore"),
Goals = match.Elements("goals").Select(ev => new MatchEvent
{
Player = (string)ev.Attribute("playerName"),
Time = (string)ev.Attribute("time")
}).ToList(),
Dismissals = match.Elements("dismissals").Select(ev => new MatchEvent
{
Player = (string)ev.Attribute("playerName"),
Time = (string)ev.Attribute("time")
}).ToList(),
};
And the updated XAML:
<ListBox Name="listBox2" Grid.Row="0">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Name="teams" Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<TextBlock Margin="0,10,0,0" Foreground="White" HorizontalAlignment="Left" Text="{Binding HomeTeam}"/>
<TextBlock Margin="10,10,30,0" Foreground="White" Text="{Binding HomeScore}" TextWrapping="Wrap" FontSize="20" />
<TextBlock Margin="0,10,0,0" Foreground="White" HorizontalAlignment="Left" Text="{Binding AwayTeam}"/>
<TextBlock Margin="10,10,30,0" Foreground="White" Text="{Binding AwayScore}" TextWrapping="Wrap" FontSize="20" />
</StackPanel>
<ItemsControl ItemsSource="{Binding Goals}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Name="scores" Orientation="Horizontal">
<TextBlock Margin="0,10,0,0" Foreground="White" Text="{Binding Player}" TextWrapping="Wrap" FontSize="20" />
<TextBlock Margin="10,10,30,0" Foreground="White" Text="{Binding Time}" TextWrapping="Wrap" FontSize="20" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<ItemsControl ItemsSource="{Binding Dismissals}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Name="scores" Orientation="Horizontal">
<TextBlock Margin="0,10,0,0" Foreground="White" Text="{Binding Player}" TextWrapping="Wrap" FontSize="20" />
<TextBlock Margin="10,10,30,0" Foreground="White" Text="{Binding Time}" TextWrapping="Wrap" FontSize="20" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

Categories