I am not able to find button click even method - c#

I am using Visual Studio 2017 and I am creating a Xamarin page.
I have some controls like label and buttons. That is below:
<StackLayout>
<Label Text="Date and Time Picker"
VerticalOptions="Center"
HorizontalOptions="Center"/>
<DatePicker x:Name="datepicker1" MaximumDate="2/2/2012" MinimumDate="1/1/2002"></DatePicker>
<Button x:Uid="btntime" Text="submit" Clicked="mybtn"></Button>
</StackLayout>
In the button, I am not able to open click event. How can I redirect the button click event?

Not sure if i understand your question correctly
but if you want the function to appear in the code behind go in you xaml, select the Clicked function and hit "F12" it will create it in the code-behind

I think it doesn't generate automatically in new version. You have to write it manually in .cs file.
public void mybtn(Object obj, EventArgs args)
{ // your code }

Related

WPF RelayCommand not firing on button click

I'm inexperienced with both WPF and MVVM so i'm most likeley missing something but when I click my button the command isn't firing. I also have some menu controls on my page that i've setup the exact same way and when I click those, their commands work as expected.
I've tried attaching a click event handler to make sure the button is definitely being clicked which it is. I've also tried attaching a different command that works on my menu control which didn't work on the button.
<Button Grid.Row="1" Content="Add Note"
Command="{Binding InsertNoteCommand}"/>
public ICommand InsertNoteCommand { get; }
public MainViewModel()
{
InsertNoteCommand = new RelayCommand(InsertNote);
}
private void InsertNote()
{
Console.WriteLine("Note Inserted!");
}
I should also mention that i'm using MVVM Light
The debugging information is very useful to know but in the end I solved the problem by pointing the binding to the data context.
<Button x:Name="AddNewNoteBtn" Grid.Row="1" Content="Add Note"
Command="{Binding Path=DataContext.InsertNoteCommand, ElementName=_window}"/>
If anybody has comments on how I can improve this I would really appreciate it. Thanks!

Is it possible to bind XAML/C# with HTML using PHP?

So I have a XAML button:
<StackPanel>
<TextBlock HorizontalAlignment="Left" Text="{Binding Version, ElementName=Control}" />
<Button Content="Support" Width="100" Click="HelpSupport_Click" />
</StackPanel>
Which links to some C#
private void HelpSupport_Click(object sender, RoutedEventArgs e)
{
System.Diagnostics.Process.Start("support.html");
}
Which takes a user to a webpage.
In my XAML StackPanel above I'm binding the software version into a text block for the user to see. But my question is, is it possible that when the user clicks the button to go to the website, can I somehow bind the text block data to be transferred into the HTML too? So that when the user gets to the webpage the software version is also showing there too? Just wondering if this would be done via PHP or a JSON call
I don't have a lot of recent experience with HTML and PHP, but if my memory doesn't fail me, you could add the text of the TextBlock as a variable at the end of the URL of the webpage (it would look like this www.webpage.com?text=version) and then from PHP use the GET method to get the value of the variable text in your URL.
You can do this with more than one variable, like this www.webpage.com?text=version&var2=hello
Managed to get it, these are the changes made:
private void HelpSupport_Click(object sender, RoutedEventArgs e)
{
System.Diagnostics.Process.Start("support.php?version=" + HttpUtility.UrlEncode(Version.ToString()));
}
Which put the number in the URL. So my output in HTML:
<textarea type="text" class="form-control" name="es_version" >
<?php echo $_GET["version"]; ?>
</textarea>

Xamarin Forms: How to access button name in XAML from C#?

I have the following XAML markup:
<ListView x:Name="wordsListView">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<StackLayout>
<Label Text="{Binding Keyword}" />
<Button Text="Click Me" x:Name="randomButton"></Button>
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
And in my .xaml.cs file I have the button clicked event for the random button defined as below:
randomButton.Clicked += (object sender, EventArgs e) =>
{
a = rand.Next(words.Count);
word = words[a];
};
But somehow when I build my project I kept getting an error: The name randomButton does not exists in the current context. Can anyone tell me what I'm doing wrong and how it should be done?
You can't access any control by name or even give it a name as long as it is on template view , naming it is not appropriate as long as this control on template will be repeated on run time!
but you can access the whole control from the Sender of the click event ... and this depends on your development approach
Hope it will help others.
There is nothing wrong with your code.You can try these 2 ways:
First, save xaml file and use this.randomButton. Try if it works.
Else Clean the solution and try again. If that didn't work, restart the Visual Studio.

How to build my own close button?

I have a wpf application with a MVVM. I am trying here to build my own close button. Based on this answer Creating a custom Close Button in WPF I added a button event handler in the View(xaml.cs) code. However, it is not recognizing the Close(); call (doesn't exist in the context - Can't resolve symbol).
Also I tried the other answer and added Command and CommandParameter into my button's xaml. But the function behind is not getting hits. In How to bind Close command to a button using the RelayCommand also my wpf is not recognizing RelayCommand. Then How can I use the RelayCommand in wpf said that I have to write it myself(really?). I remember there was a simple way similar to just set an event handler for the button and call Close();. But, how can I do that or why it is not working for me?
View code:
private void closeButton_Click(object sender, RoutedEventArgs e)
{
// I want to call close the whole app on button click
//Close(); is not recognized
}
private void performMainCloseButtonCommand(object Parameter)
{
// This doesn't get hits on button click
Window objWindow = Parameter as Window;
objWindow.Close();
}
Button XAML:
<Button x:Name="closeButton" RenderTransformOrigin="0.5,0.5" Padding="0" Margin="701,0,0,0" BorderThickness="0" Click="closeButton_Click" Command="{Binding MainCloseButtonCommand}" CommandParameter="{Binding ElementName = mainWindow}" Height="45" Width="45" >
<StackPanel Height="45" Width="45">
<Image x:Name="closeButtonImage" Margin="0" Source="/ProjectName;component/Resources/x.fw.png" Height="33"/>
<TextBlock Text="Close" Width="36" Padding="6,0,0,0" HorizontalAlignment="Center" Height="13" FontSize="10"/>
</StackPanel>
</Button>
Close isn't recognized in your event handler because there is probably no method called Close in your current class. If you want to call main window's close method you can use:
private void closeButton_Click(object sender, RoutedEventArgs e)
{
Application.Current.MainWindow.Close();
}
Above is not a good way to do this and does not align with MVVM pattern. Which relates to your second question. Without seeing remaining part of your code, its hard to say why command binding isn't working. My guess you haven't wired up the commands properly for it to fire. You will need to ensure that you have created your RelayCommand instance and your command properties are correctly set.

WPF catch Click on a grid and all of his children labels, textboxs, etc.

Lets say I have a xaml file, a window, why not. in this xaml I have a grid with multiple labels, textBoxs, comboBoxs, lists... You see the patern. At a certain point (where X == true for say) I want to be able to catch a click inside the grid and everything in it.
I want to be still able to do what this click was going to do so a full-filled Rect over the grid is not the answer I'm looking for. The action of the click would be to put X back to false. nothing much.
Is there an easy way to manage a click on a grid and everything inside it?
Thanks in advance
You just need to use an event that is common to all of the controls. Probably the best one for this scenario is the UIElement.PreviewMouseDown event. Try this:
<StackPanel UIElement.PreviewMouseDown="StackPanel_PreviewMouseDown">
<Label Content="I'm a Label" />
<Button Content="I'm a Button" />
<CheckBox Content="I'm a CheckBox" />
</StackPanel>
You need to use one of the Preview... events so that you can catch it before the Buttons consume it... the UIElement.MouseDown event wouldn't work with Buttons for that very reason. However, you can use the othwer Preview... methods, like the UIElement.PreviewLeftMouseButtonDown event, for example.
Can you give your sample code?
From my understanding,you can use this,it will capture all your click inside grid.
.xaml
<Grid MouseDown="Grid_MouseDown">
<Label MouseDown="Grid_MouseDown" />
<Button MouseDown="Grid_MouseDown"/>
<Button MouseDown="Grid_MouseDown"/>
</Grid>
.xaml.cs
private Grid_MouseDown(object sender,MouseButtonEventArgs e)
{
if(X==true)
{
//doSomething
}
else
{
//do SomethingElse
}
}
edit: How about this?

Categories

Resources