Get an object by click c# Windows 8 - c#

Sorry i'm new in C# and i'm looking everywhere, and can't find even it looks easy to do.
I want to get the object by click on it but i don't know how to do it.
A simple button in xaml :
<TextBlock
Text="{Binding ProjectName}"
VerticalAlignment="Center" Tapped="On_Tapped_Project"/>
And i Use a simple function :
private void On_Tapped_Project(object sender, TappedRoutedEventArgs e)
{
this.Frame.Navigate(typeof(NoteFolders), MyProjects[3]);
}
But i would like to have the specific project like MyProjects[x] x=(click Project).
Any ideas ?

Try this
var element = (sender as FrameworkElement);
if (element != null)
{
var project = element.DataContext as Project;
if (project != null)
{
//Implementation
}
}

You need to cast the event OriginalSource to your type (e.g. Project):
this.Frame.Navigate(typeof(NoteFolders), (Project) e.OriginalSource);

You can store project id in a tag property of a control
<TextBlock
Text="{Binding ProjectName}" Tag="{Binding ProjectID}"
VerticalAlignment="Center" Tapped="On_Tapped_Project"/>
You can recover it by access to a tag property
private void On_Tapped_Project(object sender, TappedRoutedEventArgs e)
{
var projectID = ((TextBlock)sender).Tag as int;
Frame.Navigate(typeof(NoteFolders), MyProjects[projectID]);
}

private void On_Tapped_Project(object sender, TappedRoutedEventArgs e)
{
var projectID = ((TextBlock)sender).Tag as int;
Frame.Navigate(typeof(NoteFolders), MyProjects[projectID]);
}
is a way to do it, but it is by far more recommended to do it this way:
private void On_Tapped_Project(object sender, TappedRoutedEventArgs e)
{
var projectID = (sender as TextBlock).Tag as int;
Frame.Navigate(typeof(NoteFolders), MyProjects[projectID]);
}
"sender as ElementType" is the correct way to cast sender, other methods can cause minor issues, always do the best code and never wonder why it went wrong that one time.

Related

WPF slider thumb.DragCompleted

I'm learning from Mark Heath course of NAudio.
I'm using a slider with the event:
Thumb.DragCompleted="SilderPositionOnDragCompleted"
And in the c#:
private void SilderPositionOnDragCompleted(object sender, System.Windows.Controls.Primitives.Thumb.DragCompleted e)
{
if (reader != null)
{
reader.CurrentTime = TimeSpan.FromSeconds(slider.Value);
}
}
I'm pretty sure that I wrote wrong the:
System.Windows.Controls.Primitives.Thumb.DragCompleted
Because I have no idea what I need to write in there - I saw this here in the site.
Here are the errors.
What do I need to do?
Thanks!
Try this:
private void SilderPositionOnDragCompleted(object sender, RoutedEventArgs e)
{
if (reader != null)
{
reader.CurrentTime = TimeSpan.FromSeconds(slider.Value);
}
}
The type of the second argument should be RoutedEventArgs.
And if you are hooking up the event handler programmatically you should use the following syntax:
Thumb.DragCompleted += SilderPositionOnDragCompleted;
...where "Thumb" is the name of your Thumb:
<Thumb x:Name="Thumb" />
Or
Thumb Thumb = ...;

How to transfer textbox to another page in Windows Phone 8.1-C#

Hi guys I am new to app development and was wondeing how to transfer a textbox that holds a value to another page. My code is below:
Basket Button
private void Confirm_Click(object sender, RoutedEventArgs e)
{
Frame.Navigate(typeof(Confirmation) , Totaltxt_TextChanged = TotalValue);
}
The OnNav in which information needs being sent to
protected override void OnNavigatedTo(NavigationEventArgs e)
{
Basket Basket = e.Parameter as Basket;
if (Basket != null)
{
COsttxt.Text = string.Format("{0:C}", Basket.Totaltxt_TextChanged);
}
}
I have tried with just TotalValue which is the main value that needs to be sent but that seem to work I even set it in the textbox of the NavTo page:
<TextBox x:Name="COsttxt" HorizontalAlignment="Left" Height="58" Margin="229,258,0,0" TextWrapping="Wrap" Text="{Binding TotalValue}" VerticalAlignment="Top" Width="131" TextChanged="COsttxt_TextChanged"/>
So is there a way to just send the Totaltxt textbox from Basket.xaml and make the textbox value display in Confirmation.xaml?
You are passing a parameter to Confirmation page which I am sure is not of type Basket and hence you are getting null when you do as conversion.
You should be simply passing/reading TotalValue this way:
private void Confirm_Click(object sender, RoutedEventArgs e)
{
Frame.Navigate(typeof(Confirmation) , TotalValue);
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
var totalValue = e.Parameter as string; // or 'as' whatever is type of TotalValue
if (totalValue != null)
{
COsttxt.Text = string.Format("{0:C}", totalValue);
}
}
Edit: Pass the final textbox value as below and keep rest of the code same:
Frame.Navigate(typeof(Confirmation) , textboxName.Text);

Open and Close the same window on the same button click

On my MenuItem at the moment I open up a Window. Then if the person chooses to click on that MenuItem again, I need the Window that was open to close.
Then obviously, if they click it the third time it will open and so fourth.
XAML
<MenuItem x:Name="btnHelp" Click="btnHelp_Click" Foreground="#FF7E8385" FontFamily="Calibri" FontSize="18" Margin="110,10,0,0" Height="30" Width="70" Style="{x:Null}" BorderBrush="Transparent" Background="Transparent" Cursor="Hand"/>
Code behind
private void btnHelp_Click(object sender, RoutedEventArgs e)
{
xamlHelp help = new xamlHelp();
help.Show();
}
You'll need to have the variable be an instance field, rather than a local variable, so it can be accessed between calls. At that point just close it if it exists, and recreate/show it if it doesn't:
private xamlHelp help = null;
private void btnHelp_Click(object sender, RoutedEventArgs e)
{
if (help != null)
{
help.Close();
help = null;
}
else
{
help = new xamlHelp();
help.Show();
}
}
You can keep track of whether the help is currently showing and show/hide based on that,
private xamlHelp help = new xamlHelp();
private bool showingHelp = false;
private void btnHelp_Click(object sender, RoutedEventArgs e)
{
if (showingHelp)
{
help.Hide();
}
else
{
help.Show();
}
}

How to display checkbox value and text in WPF?

I need to display value and text contained in the particular CheckBox which is checked on its checked event in WPF. How to do that?
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
MessageBox.Show(........need help here......);
}
I'm not sure I understood your expectations. You want to retrieve the value "checked" - "unchecked" from the checkBox?
So can you try this?
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
//Get the boolean current value [true or false]
bool valueSelectedToBool = (sender as CheckBox).IsChecked;
//Get the string current value ["true" or "false"]
string valueSelectedToString = (sender as CheckBox).IsChecked.ToString();
MessageBox.Show(valueSelectedToString );
}
You could try this :
I don't know if you want the uncheck action to trigger the event but I put it.
In XAML :
<CheckBox Content="CheckBox" VerticalAlignment="Top" Unchecked="CheckBox_Checked_1" Checked="CheckBox_Checked_1"/>
In C# :
private void CheckBox_Checked_1(object sender, RoutedEventArgs e)
{
CheckBox check = sender as CheckBox;
MessageBox.Show(check.IsChecked.Value.ToString());
}
Just tested it, it works.
I hope it is what you are searching for.
I'm working with WPF.
I used these lines of code. In my case it works properly.
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
bool Chked = Convert.ToBoolean((sender as CheckBox).IsChecked);
string ChkBoxContent = (sender as CheckBox).Content.ToString();
TxtHabitsHx.AppendText(ChkBoxContent);
}

How do I save the Checked property state to Properties.Settings When using WPF?

I'm converting a program from WinForms to WPF. There seems to be a lot of unnecessary syntax changes. But the one I'm having trouble with is saving the "checked" or "unchecked" status to Properties.Settings. In WinForms I used:
private void chkBackup_CheckedChanged(object sender, EventArgs e)
{
Properties.Settings.Default.Backup = chkBackup.Checked;
Properties.Settings.Default.Save();
}
There doesn't seem to be an Event for "CheckedChanged" in WPF, So I'm trying:
private void chkBackup_Checked(object sender, RoutedEventArgs e)
{
Properties.Settings.Default.Backup = (chkBackup.IsChecked == true);
Properties.Settings.Default.Save();
}
private void chkBackup_Unchecked(object sender, RoutedEventArgs e)
{
Properties.Settings.Default.Backup = (chkBackup.IsChecked == false);
Properties.Settings.Default.Save();
}
I get no errors with this, but when I uncheck the checkBox, the settings aren't changed. Please help. What am I doing wrong.
Thanks
You're using a different expression each time. In the checked event you're using chkBackup.IsChecked == true which evaluates to true if the box is checked and false otherwise.
In the unchecked event you're using chkBackup.IsChecked == false which evaluates to true if the box isn't checked and false otherwise.
What you're interested in is if the box is checked or not. The expression to use for this is chkBackup.IsChecked == true. Your current solution will always save true.
you are using the Settings object correctly (from the code you provided), so maybe you need to attach the Checked/Unchecked event handlers.... (MSDN here)
<!-- in your xaml -->
<CheckBox Checked="OnChecked" Unchecked="OnUnchecked"/>
//in your code-behind....
myCheckbox.OnChecked += myHandler;
Your code looks fine. Are you hooking up your handlers in XAML?
<CheckBox Name="chkbox"
Content="Some Checkbox"
Checked="chkBackup_Checked"
Unchecked="chkBackup_Unchecked" />
If you want it to be a little more streamlined, you could do something like this:
<CheckBox Name="chkbox"
Content="Some Checkbox"
Checked="chkBackup_CheckChanged"
Unchecked="chkBackup_CheckChanged" />
private void chkBackup_CheckChanged(object sender, RoutedEventArgs e)
{
Properties.Settings.Default.Backup = chkBackup.IsChecked;
Properties.Settings.Default.Save();
}
I've found, that in WPF you can use strings to save a value. It's inconvenient, but it works. :/
private void chkBackup_Checked(object sender, RoutedEventArgs e)
{
Properties.Settings.Default.Backup = "true";
Properties.Settings.Default.Save();
}
private void chkBackup_Unchecked(object sender, RoutedEventArgs e)
{
Properties.Settings.Default.Backup = "false";
Properties.Settings.Default.Save();
}
and to check if it's checked, you can use
if (Properties.Settings.Default.Backup == "false")
{
//enter code here
}

Categories