Like you can see here, i got a button with Name, etc. and i got in this button a usercontrol. When i click on the button everything is alright, but when i programmaticaly click this button. The UserControls will not be performed... why?
<Button x:Name="btnHome" Style="{DynamicResource PopupButtonStyle}" MouseEnter="btnHome_MouseEnter" MouseLeave="btnHome_MouseLeave" Click="btnHome_Click">
<ctls:MenuItem GroupName="MenuItem" IndicatorBrush="{DynamicResource PrimaryBlueColor}" Icon="{DynamicResource home}" IconWidth="16" Text="Home" VerticalAlignment="Center"/>
</Button>
This is the code for clicking the button, am i wrong here?
private void Window_Loaded(object sender, RoutedEventArgs e)
{
btnHome.RaiseEvent(new RoutedEventArgs(ButtonBase.ClickEvent));
}
private void btnHome_Click(object sender, RoutedEventArgs e)
{
}
Related
I created some buttons with another button inside. Then I want to click the inner button but it only throws me the outer button click event, not even both. I tried the solutions in this question but didn't achieve anything. The exact goal that I have is to only load the click method of the inner button when i click it, and if I click in wherever else in the outer button it throws me the respective method.
//Outer button click event
void newBtn_Click(object sender, EventArgs e) {
//Stuff
}
newBtn.Click += new RoutedEventHandler(newBtn_Click);
//Inner button click event
void editButton_Click(object sender, RoutedEventArgs e)
{
//Stuff
editButton.Click += new RoutedEventHandler(editButton_Click);
e.Handled = true;
}
//stuff
It would be better to share your code asking a question.
By the way this sample should work:
<Button Background="Red" Width="100" Height="50" Click="Button_Click">
<Button Background="Green" Margin="10" Width="50" Height="50" Click="Button_Click_1" />
</Button>
In the event handler of the inner button simply add this, as suggested by the post you mentioned:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
e.Handled = true;
}
This will stop the chain of Routed Events (Bubbling and Tunneling)
I have set the initial resize mode of the window to be "NoResize" as shown below.
<Widnow ... WindowStyle="None" ResizeMode="NoResize">
I want to change the resize mode to "CanResizeWithGrip" after this button is clicked.
<Button x:Name="btnResize" Content="Click Me!" Click="BtnResize_Click"/>
And this is my event.
private void BtnResize_Click(object sender, RoutedEventArgs e)
{
}
private void BtnResize_Click(object sender, RoutedEventArgs e)
{
this.ResizeMode = ResizeMode.CanResizeWithGrip;
}
I've struggled with this setup for couple of days and found a somewhat solution but I think it not how it supposed to work.
Here is my xaml page setup:
<Page
...
<SplitView IsPaneOpen="True" DisplayMode="Inline" OpenPaneLength="300">
<SplitView.Pane>
<Grid>
<ToggleButton x:Name="Edit" IsEnabled="False" Checked="Edit_Checked" Unchecked="Edit_Unchecked"/>
</Grid>
</SplitView.Pane>
<Frame x:Name="RightFrame">
</Frame>
</SplitView>
</Page>
Code for toggle button:
private void Edit_Checked(object sender, RoutedEventArgs e)
{
RightFrame.Navigate(typeof(SubPage1));
}
private void Edit_Unchecked(object sender, RoutedEventArgs e)
{
RightFrame.Navigate(typeof(SubPage2));
}
So basically the toggle button switches the splitview content. The SubPages are just blank pages. The problem is that app crashes when navigation occurs.
I've noticed when I put this before invoking navigation:
RightFrame.Content = null;
Thread.Sleep(1000);
Then everything works fine. So I need to clear frame content and wait for it to finish I guess. But I think it should be done automatically. Could some one explain what am I doing wrong here and how it should be done?
Please verify your code in SubPage1 and SubPage2 which might be creating the crashes, you can clear the resources when the page gets unloaded.
Alternatively, you can try below code:
private void Edit_Checked(object sender, RoutedEventArgs e)
{
RightFrame.Content = new SubPage1();
}
private void Edit_Unchecked(object sender, RoutedEventArgs e)
{
RightFrame.Content = new SubPage2();
}
After I touch a control, such as Button, How can I get this button's name?
If you mean "click" instead of "touch", put something like this in your View XAML file:
<Button x:Name="MyButton" MouseDown="MyButton_MouseDown">
Then add following lines to the View CODE file:
void MyButton_MouseDown(object sender, MouseButtonEventArgs e) {
base.OnMouseUp(e);
var control = sender as FrameworkElement;
Console.WriteLine(control.Name);
}
When you click on a control you are raising an event which called : Control_Click
Then you can do whatever you want in that even. As you asked about button I'm gonna show you how you can get button's name . You can have the similar code for other controls :
private void button1_Click(object sender, EventArgs e)
{
string control_name = button1.Name;
MessageBox.Show(control_name);
}
I can't get this straight: I have one button that I want one action for the press event and one action for release, I've searched everywhere and can't find an answer.
KeyDown, KeyUp or MouseLeftButtonDown doesn't work with button on windows phone 7.
First I tried combining GotFocus and Click clickmode release like this:
(As you can see I want Image1 to be shown while pressing button, and hidden when releasing the button)
xaml:
Button Click="button1_Click" ClickMode="Release" GotFocus="button1_GotFocus" Content="byt" Height="72" Margin="0,500,6,0" Name="button1" VerticalAlignment="Top"
private void button1_GotFocus(object sender, RoutedEventArgs e)
{
Image1.Visibility = System.Windows.Visibility.Collapsed;
}
private void button1_Click (object sender, RoutedEventArgs e)
{
Image1.Visibility = System.Windows.Visibility.Visible;
}
This works only one time and could work all the time if I could loose focus from the button when releasing it (tried searching for that as well)
The other thing I tried was changing the clickmode while pressing the button, but didn't get that to work either..
something like this:
private void button1_Click (object sender, RoutedEventArgs e)
{
Image1.Visibility = System.Windows.Visibility.Collapsed;
button1.SetValue(Button.ClickModeProperty, ClickMode.Release);
Image1.Visibility = System.Windows.Visibility.Visible;
}
(I know that the syntax is wrong somehow in the second one)
Would be grateful for help!
MouseLeftButtonDown / MouseLeftButtonUp do work on WP7. Obviously not named the best, but they do work on the device.
<TextBlock x:Name="ApplicationTitle" MouseLeftButtonDown="ApplicationTitle_MouseLeftButtonDown" MouseLeftButtonUp="ApplicationTitle_MouseLeftButtonUp"
Style="{StaticResource PhoneTextNormalStyle}"
Text="MY APPLICATION" />
You'll see Down gets fired, and then immediately Up.
private void ApplicationTitle_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
}
private void ApplicationTitle_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
}
Perhaps all you need is MouseLeftButtonDown/Up.
I used
Click="{x:Bind UpArrow_Click}"
ClickMode="Press"
PointerCaptureLost="Button_Release"
on a button. When button is pressed UpArrow_Click event occurs... when button is released Button_Release event occurs