I'm stuck with not being able to display random generated numbers on a pivot page. Below is my code,
<!--LayoutRoot is the root grid where all page content is placed-->
<ScrollViewer>
<Grid x:Name="LayoutRoot" Background="Transparent" Height="768" Width="480">
<!--Pivot Control-->
<phone:Pivot Title="MY APPLICATION" Loaded="Pivot_Loaded">
<!--Pivot item one-->
<phone:PivotItem Header="Stats">
<Border
BorderBrush="{StaticResource PhoneBackgroundBrush}"
BorderThickness="{StaticResource PhoneBorderThickness}" Background="Black">
<Grid Margin="-3,75,-17,-75">
<TextBlock
TextWrapping="Wrap"
Style="{StaticResource PhoneTextExtraLargeStyle}" Margin="10,121,253,422" FontSize="30" Text="Highest Temp : " />
<TextBlock
TextWrapping="Wrap"
Style="{StaticResource PhoneTextExtraLargeStyle}" Margin="10,0,253,551" Text="Current Temp :" FontSize="30" />
<TextBlock
TextWrapping="Wrap"
Style="{StaticResource PhoneTextExtraLargeStyle}" Margin="10,240,253,311" Text="Lowest Temp : " FontSize="30" />
<TextBlock
TextWrapping="Wrap"
Style="{StaticResource PhoneTextExtraLargeStyle}" Margin="10,360,253,191" Text="Average Temp : " FontSize="30" />
<TextBlock Name="TempAverage"
TextWrapping="Wrap"
Style="{StaticResource PhoneTextExtraLargeStyle}" Margin="222,360,41,191" Text="-" FontSize="30" />
<TextBlock Name="TempLowest"
TextWrapping="Wrap"
Style="{StaticResource PhoneTextExtraLargeStyle}" Margin="222,240,41,311" Text="-" FontSize="30" />
<TextBlock Name="TempHighest"
TextWrapping="Wrap"
Style="{StaticResource PhoneTextExtraLargeStyle}" Margin="222,121,41,430" Text="-" FontSize="30" />
<TextBlock Name="TempCurrent"
TextWrapping="Wrap"
Style="{StaticResource PhoneTextExtraLargeStyle}" Margin="222,0,41,551" Text="-" FontSize="30" />
</Grid>
</Border>
</phone:PivotItem>
It then tells me the error "The name 'TempAverage does not exist in the current context'. Same with TempCurrent, TempHighest and TempLowest. What is the error that I am facing and how should I go about it?
int a = 0;
void time_Tick(object sender, EventArgs e)
{
DispatcherTimer time = new DispatcherTimer();
Random rnd = new Random();
double TempRandom = rnd.Next(100, 500); //generate random number
double TempRandom2 = TempRandom / 10;
***TempCurrent.Text = TempRandom2.ToString("0.0");
if(TempRandom2 > Max)
{
Max = TempRandom2; //Highest Temp = Current Temp
TempHighest.Text = Max.ToString("0.0");
}
if(TempRandom2 < Min)
{
Min = TempRandom2;
TempLowest.Text = Min.ToString("0.0"); //Lowest Temp = Current Temp
}
AvgTemp[a++ % 100] = TempRandom2;
if (a > 100)
TempAverage.Text = AvgTemp.Average().ToString("0.0");
else
TempAverage.Text = (AvgTemp.Sum() / a).ToString("0.0");***
}
You need to add a name to your elements. Eg. `TextBlocks. When you add a name to the elements you can reference them in the code. Like this.
<TextBlock Name="TempCurrent"
TextWrapping="Wrap"
Style="{StaticResource PhoneTextExtraLargeStyle}"
Margin="10,0,253,551"
Text="Current Temp :"
FontSize="30" />
Related
I have a ListBox including an ItemTemplate with a StackPanel. I want to access that stackpanel and change its visibility.
(Change it's visibility to collapsed when I click mouseleftbutton "closeAll")
I can do that with FindDescendantByName Method but it works for only listbox items on screen (Only first 10 items) but when I am scrolling down, I see that this is not working for other listbox items.
I think that errors occurs because of VisualTreeHelper. What can I use instead of VisualTreeHelper?
Thanks..
XAML CODE
<ListBox x:Name="listBoxEditPast" SelectionMode="Single" Margin="0" Background="#272B34" ScrollViewer.VerticalScrollBarVisibility="Visible">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Border Grid.Row="0" BorderThickness="4,0,0,0" Margin="2,0,0,0" Height="29" Background="#2E323B" Width="1050" BorderBrush="#1373A9" MouseLeftButtonDown="Border_MouseLeftButtonDown">
<DockPanel Name="dockPanelPast" Margin="0,4,0,0">
<Image Name="imgArrow" Source="images/down-arrow.png" HorizontalAlignment="Left" Width="20" Height="18"/>
<TextBlock Text="{Binding CreateDate}" Name="txtTarih" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="16"/>
<TextBlock Text="{Binding SarjNo}" Name="txtSarjNo" Foreground="#FF9CA518" HorizontalAlignment="Stretch" VerticalAlignment="Center" FontSize="16" Margin="50,0,0,0" Width="90"/>
<TextBlock Text="{Binding Adi}" Name="txtReceteAdi" Foreground="#FF26A053" VerticalAlignment="Center" FontSize="16" Margin="40,0,0,0" HorizontalAlignment="Stretch"/>
<Button Content="Detaylar" Style="{StaticResource BlueButton}" HorizontalAlignment="Right" VerticalAlignment="Center" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" DockPanel.Dock="Right"/>
</DockPanel>
</Border>
<StackPanel Grid.Row="1" Name="stackPanelDetay" Tag="{Binding ID}">
<DockPanel>
<TextBlock Text="Sipariş No" Foreground="#D9480F" VerticalAlignment="Center" />
<TextBlock Text="Parça" Foreground="#AF0FD9" VerticalAlignment="Center" Margin="50,0,0,0" Width="200" />
<TextBlock Text="Malzeme" Foreground="White" VerticalAlignment="Center" Margin="150,0,0,0" Width="90"/>
<TextBlock Text="Müşteri" Foreground="#AF0FD9" VerticalAlignment="Center" Margin="70,0,0,0" />
</DockPanel>
<DockPanel>
<TextBlock Text="{Binding ID}" Foreground="White" VerticalAlignment="Center" Width="100"/>
<TextBlock Text="{Binding ParcaKoduAdi}" Foreground="White" VerticalAlignment="Center" Margin="5,0,0,0" Width="200" />
<TextBlock Text="{Binding Malzeme}" Foreground="White" VerticalAlignment="Center" Margin="152,0,0,0" Width="90" />
<TextBlock Text="{Binding MusteriKoduAdi}" Foreground="White" VerticalAlignment="Center" Margin="70,0,0,0" />
</DockPanel>
</StackPanel>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
C# CODE
public static class FrameworkElementExtensions
{
public static FrameworkElement FindDescendantByName(this FrameworkElement element, string name)
{
if (element == null || string.IsNullOrWhiteSpace(name)) { return null; }
if (name.Equals(element.Name, StringComparison.OrdinalIgnoreCase))
{
return element;
}
var childCount = VisualTreeHelper.GetChildrenCount(element);
for (int i = 0; i < childCount; i++)
{
var result = (VisualTreeHelper.GetChild(element, i) as FrameworkElement).FindDescendantByName(name);
if (result != null) { return result; }
}
return null;
}
}
private void closeAll_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
// StackPanel panel = LayoutHelper.FindElement(listBoxEditPast, n => n.GetType() == typeof(StackPanel)) as StackPanel;
for (int i = 0; i < listBoxEditPast.Items.Count; i++)
{
var element = listBoxEditPast.ItemContainerGenerator.ContainerFromIndex(i) as FrameworkElement;
if (element != null)
{
var sp = element.FindDescendantByName("stackPanelDetay") as StackPanel;
if (sp != null)
{
sp.Visibility = Visibility.Collapsed;
}
}
}
}
noting to do with the visualtreehelper, this is because the list is virtualized, so only the first ten items are created and then replaced by the ten next....and you loose your modifications
you must not work with the element in the data template by code
iterate through your data to set a boolean to true/false for all and then change the stack and bind the visibility to this boolean
<StackPanel Grid.Row="1" Name="stackPanelDetay" Visibility="{Binding myBoolean, Converter=BoolToVisibility}">
On my Resources.xaml(Resource Dictionary) I have a DataTemplate :
<DataTemplate x:Key="ProductDetailsContentTemplate">
<StackPanel Orientation="Vertical" >
<Viewbox Margin="27,0,28,0" Height="200" >
<Image UWPUtil:ImageExtensions.CachedUriSource="{Binding ImageUri}" />
</Viewbox>
<TextBlock Text="Description" Style="{StaticResource CaptionTextBlockStyle}" FontWeight="Bold" FontSize="24"/>
<TextBlock Text="{Binding ProductDescription}" Style="{StaticResource SubtitleTextBlockStyle}" FontSize="30"/>
<TextBlock Text="Barcode" Style="{StaticResource CaptionTextBlockStyle}" FontWeight="Bold" FontSize="24"/>
<TextBlock Text="{Binding Barcode}" Style="{StaticResource SubtitleTextBlockStyle}" FontSize="30"/>
<TextBlock Text="Weight" Style="{StaticResource CaptionTextBlockStyle}" FontWeight="Bold" FontSize="24"/>
<TextBlock Text="{Binding Weight}" Style="{StaticResource SubtitleTextBlockStyle}" FontSize="30"/>
<TextBlock Text="Quantity" Style="{StaticResource CaptionTextBlockStyle}" FontWeight="Bold" FontSize="24"/>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="25*"/>
<ColumnDefinition Width="50*"/>
<ColumnDefinition Width="25*"/>
</Grid.ColumnDefinitions>
<Button x:Name="SubtractQytyButton" Grid.Column="0" Content="-" FontSize="24" HorizontalAlignment="Stretch"
Margin="5,0" Style="{StaticResource TestButtonStyle}" FontWeight="ExtraBold"
Click="SubQytyButton_Click"/>
<TextBox x:Name="QuantityTextBox" Grid.Column="1" HorizontalAlignment="Stretch" FontSize="24" Margin="5,0"
KeyDown="Quantity_Keydown" TextChanging="Quantity_TextChanging"
Text="{Binding Quantity, Mode=TwoWay, Converter={StaticResource DecmialConverter}}" MaxLength="5" TextAlignment="Center"/>
<Button x:Name="AddQytyButton" Grid.Column="2" Content="+" FontSize="24" HorizontalAlignment="Stretch"
Margin="5,0" Style="{StaticResource TestButtonStyle}" FontWeight="ExtraBold"
Click="AddQytyButton_Click"/>
</Grid>
</StackPanel>
</DataTemplate><br>
I file nested it using the File Nesting from Mads Kristensen , here is the thread: https://marketplace.visualstudio.com/items?itemName=MadsKristensen.FileNesting
so I have a Resources.xaml.cs on my ResourceDictionary
my problem is I want to set my QuantityTextBox Value on AddQytyButton and SubQytyButton click command, here is my click command event:
private void AddQytyButton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
//does not exist in the current context
QuantityTextBox.Text = "test";
}
OR
private void AddQytyButton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
Button btn = sender as Button;
// this is not working as null reference
var textbox = (btn.Parent as StackPanel).Children[1] as TextBox;
}
Thanks,
NicoTing
You should cast btn.Parent to a Grid instead of a StackPanel since you are using the former in your XAML.
var textbox = (btn.Parent as Grid).Children[1] as TextBox;
There is a ContestListView with elements:
public MainPage()
{
this.InitializeComponent();
List<int> list = new List<int>();
list.Add(2);
list.Add(3);
list.Add(5);
list.Add(7);
ContestListView.ItemsSource = list;
}
I want to navigate to another Frame when SelectionChange is.
private void ContestSelectionChange(object sender, SelectionChangedEventArgs e)
{
this.Frame.Navigate(typeof(BlankPage1));
}
But I have a problem - sometimes, when I choose element at list, the BlankPage1 starts load, but I got an unhandled exception
{"Unspecified error\r\n\r\nUnspecified error\r\n"}
at this place
#if DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
UnhandledException += (sender, e) =>
{
if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break();
};
#endif
This exception can be after first SelectionChandeg, but it can be too after second, third etc.
There is BlankPage1 XAML:
<Page
x:Class="Contests.BlankPage1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Contests"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.TopAppBar>
<CommandBar Foreground="White">
<CommandBar.Background>
<ImageBrush Stretch="Fill" ImageSource="Assets/app02_0013_panel_main.png"/>
</CommandBar.Background>
<CommandBar.SecondaryCommands>
<AppBarButton Label="LogOut" Click="onClick"/>
</CommandBar.SecondaryCommands>
</CommandBar>
</Page.TopAppBar>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ScrollViewer>
<RelativePanel>
<TextBlock x:Name="txtContestName"
Text="TestTest "
Foreground="DarkOliveGreen"
RelativePanel.AlignLeftWithPanel="True"
RelativePanel.Below=""
TextWrapping="Wrap"
FontSize="20"
Margin="5,10,0,10"/>
<TextBlock x:Name="CategoryExpiration" FontSize="12"
RelativePanel.AlignLeftWithPanel="True"
RelativePanel.Below="txtContestName"
Margin="5,0,0,10">
<Run x:Name="txtCategory" Text="Test "/>
<Run x:Name="txtExpiration" Text="2016-05-23" Foreground="Green"/>
</TextBlock>
<RelativePanel x:Name="QuestionPanel" RelativePanel.Below="CategoryExpiration"
RelativePanel.AlignLeftWithPanel="True"
RelativePanel.AlignRightWithPanel="True"
Background="Green"
Margin="5,0,5,5">
<TextBlock x:Name="textblock1" Text="Question:" Foreground="White"
RelativePanel.AlignLeftWithPanel="True"
Margin="5,5,5,0"/>
<TextBlock x:Name="txtQuestion" Foreground="White"
Text="Test?"
RelativePanel.AlignHorizontalCenterWithPanel="True"
RelativePanel.Below="textblock1"
TextWrapping="Wrap"
Margin="10, 5" TextAlignment="Center"/>
<TextBox x:Name="AnswerText" PlaceholderText="Test"
RelativePanel.Below="txtQuestion"
RelativePanel.AlignRightWithPanel="True"
RelativePanel.AlignLeftWithPanel="True"
Margin="5,5"/>
<Button x:Name="bSendAnswer" Content="Test"
RelativePanel.Below="AnswerText"
RelativePanel.AlignHorizontalCenterWithPanel="True"
Foreground="White"
Margin="5,5" Width="335"/>
</RelativePanel>
<RelativePanel x:Name="OrgPanel" RelativePanel.Below="QuestionPanel"
RelativePanel.AlignLeftWithPanel="True"
RelativePanel.AlignRightWithPanel="True"
Margin="5,0,5,5" BorderBrush="Gray">
<TextBlock x:Name="textblock2" Text="Organizator" Foreground="Green"
RelativePanel.AlignLeftWithPanel="True"
Margin="5,5,5,0"/>
<TextBlock x:Name="Org" Text="Test"
RelativePanel.Below="textblock2"
Foreground="DarkGreen"
FontSize="12"
RelativePanel.AlignRightWithPanel="True"
RelativePanel.AlignLeftWithPanel="True"
Margin="5,5"/>
</RelativePanel>
<RelativePanel x:Name="DesriptionPanel" RelativePanel.Below="OrgPanel"
RelativePanel.AlignLeftWithPanel="True"
RelativePanel.AlignRightWithPanel="True"
Margin="5,0,5,5" BorderBrush="Gray">
<TextBlock x:Name="textblock3" Text="Opis konkursu" Foreground="Green"
RelativePanel.AlignLeftWithPanel="True"
Margin="5,5,5,0"/>
<TextBlock x:Name="txtDescription" Text="test"
RelativePanel.Below="textblock3"
TextWrapping="Wrap"
Foreground="DarkGreen"
FontSize="12"
RelativePanel.AlignRightWithPanel="True"
RelativePanel.AlignLeftWithPanel="True"
Margin="5,5"/>
</RelativePanel>
<RelativePanel x:Name="PrizePanel" RelativePanel.Below="DesriptionPanel"
RelativePanel.AlignLeftWithPanel="True"
RelativePanel.AlignRightWithPanel="True"
Margin="5,0,5,5" BorderBrush="Gray">
<TextBlock x:Name="textblock4" Text="Test" Foreground="Green"
RelativePanel.AlignLeftWithPanel="True"
Margin="5,5,5,0"/>
<TextBlock x:Name="txtPrize" Text=""
RelativePanel.Below="textblock4"
Foreground="DarkGreen"
TextWrapping="Wrap"
FontSize="12"
RelativePanel.AlignRightWithPanel="True"
RelativePanel.AlignLeftWithPanel="True"
Margin="5,5"/>
</RelativePanel>
</RelativePanel>
</ScrollViewer>
</Grid>
What my problem is?
I wrote function for droping list items:
private void grid1_Drop(object sender, RoutedEventArgs e)
{
TextBlock textBlock = e.Source as TextBlock;
Console.WriteLine("drop item into grid column:{0} row:{1}",
Grid.GetColumn(textBlock), Grid.GetRow(textBlock));
DataObject item = (((DragEventArgs)e).Data) as DataObject;
ListBoxItem listItem = item.GetData(typeof(ListBoxItem)) as ListBoxItem;
Window1 second = new Window1();
second.ShowDialog();
//textBlock.Height = textBlock.Height*second.getBrCasova();
Grid.SetRowSpan(textBlock, second.getBrCasova());
textBlock.Background = Brushes.Gray;
string pom = "";
if(second.getBrCasova() == 1)
textBlock.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
else
for (int i = 0; i < second.getBrCasova();i++ )
pom += "\n";
textBlock.Text = pom + listItem.Content.ToString() + "\n" + second.getUcionica();
textBlock.TextAlignment = System.Windows.TextAlignment.Center;
textBlock.Margin = new Thickness(0,0,2,2.5);
}
the problem is, if I get Grid.SetRowSpan(textBlock, second.getBrCasova()), second.getBrCasova>2, borders between cells wont be deleted.
This part of xaml show how I make table of textblocks:
<Border Grid.Column="1" Grid.Row="1" BorderBrush="Black" BorderThickness="0,0,2,2" Background="Transparent" />
<Border Grid.Column="2" Grid.Row="1" BorderBrush="Black" BorderThickness="0,0,2,2" Background="Transparent" />
<Border Grid.Column="3" Grid.Row="1" BorderBrush="Black" BorderThickness="0,0,2,2" Background="Transparent" />
<Border Grid.Column="4" Grid.Row="1" BorderBrush="Black" BorderThickness="0,0,2,2" Background="Transparent" />
<Border Grid.Column="5" Grid.Row="1" BorderBrush="Black" BorderThickness="0,0,2,2" Background="Transparent" />
<Border Grid.Column="6" Grid.Row="1" BorderBrush="Black" BorderThickness="0,0,2,2" Background="Transparent" />
<TextBlock Grid.Row="1" Grid.Column="1" Name="TextBlock11" AllowDrop="True" PreviewMouseLeftButtonDown="TextBlock11_PreviewMouseLeftButtonDown"></TextBlock>
<!--<Rectangle Grid.Column="2"
Grid.Row="1"
Stroke="Black"
Fill="Transparent" />-->
<TextBlock Grid.Row="1" Grid.Column="2" AllowDrop="True"></TextBlock>
<!--<Rectangle Grid.Column="3"
Grid.Row="1"
Stroke="Black"
Fill="Transparent" />-->
<TextBlock Grid.Row="1" Grid.Column="3" AllowDrop="True"></TextBlock>
<!--<Rectangle Grid.Column="4"
Grid.Row="1"
Stroke="Black"
Fill="Transparent" />-->
<TextBlock Grid.Row="1" Grid.Column="4" AllowDrop="True"></TextBlock>
....
How to remove this borders from function?
Thanks in advance.
I am currently using multiple textblocks on a pivot control page, but I am unable to change the value of my textblocks. The reason is being "The name 'XXX' does not exist in the current context" even though I have named my textblocks for the value to be written in.
I am able to change the values when using a normal application page but pivot does not allow me to. Is there any way I can get past this?
<ScrollViewer>
<Grid x:Name="LayoutRoot" Background="Transparent" Height="768" Width="480">
<!--Pivot Control-->
<phone:Pivot Title="MY APPLICATION" Loaded="Pivot_Loaded">
<!--Pivot item one-->
<phone:PivotItem Header="Stats">
<Border
BorderBrush="{StaticResource PhoneBackgroundBrush}"
BorderThickness="{StaticResource PhoneBorderThickness}" Background="Black">
<Grid Margin="-3,75,-17,-75">
<TextBlock
TextWrapping="Wrap"
Style="{StaticResource PhoneTextExtraLargeStyle}" Margin="10,121,253,422" FontSize="30" Text="Highest Temp : " />
<TextBlock
TextWrapping="Wrap"
Style="{StaticResource PhoneTextExtraLargeStyle}" Margin="10,0,253,551" Text="Current Temp :" FontSize="30" />
<TextBlock
TextWrapping="Wrap"
Style="{StaticResource PhoneTextExtraLargeStyle}" Margin="10,240,253,311" Text="Lowest Temp : " FontSize="30" />
<TextBlock
TextWrapping="Wrap"
Style="{StaticResource PhoneTextExtraLargeStyle}" Margin="10,360,253,191" Text="Average Temp : " FontSize="30" />
<TextBlock x:Name="TempAverage"
TextWrapping="Wrap"
Style="{StaticResource PhoneTextExtraLargeStyle}" Margin="222,360,41,191" Text="-" FontSize="30" />
<TextBlock x:Name="TempLowest"
TextWrapping="Wrap"
Style="{StaticResource PhoneTextExtraLargeStyle}" Margin="222,240,41,311" Text="-" FontSize="30" />
<TextBlock x:Name="TempHighest"
TextWrapping="Wrap"
Style="{StaticResource PhoneTextExtraLargeStyle}" Margin="222,121,41,430" Text="-" FontSize="30" />
<TextBlock x:Name="TempCurrent"
TextWrapping="Wrap"
Style="{StaticResource PhoneTextExtraLargeStyle}" Margin="222,0,41,551" Text="-" FontSize="30" />
</Grid>
</Border>
Here is my cs code
if(TempRandom2 > Max)
{
Max = TempRandom2; //Highest Temp = Current Temp
TempHighest.Text = Max.ToString("0.0");
}
if(TempRandom2 < Min)
{
Min = TempRandom2;
TempLowest.Text = Min.ToString("0.0"); //Lowest Temp = Current Temp
}
AvgTemp[a++ % 100] = TempRandom2;
if (a > 100)
TempAverage.Text = AvgTemp.Average().ToString("0.0");
else
TempAverage.Text = (AvgTemp.Sum() / a).ToString("0.0");
}
}
}