UserControl tab focus not working - c#

I got a class that extends UserControl
I have a few tabs in it, and I want to recognise when a tab is selected, so I have this:
this.GotFocus += new RoutedEventHandler(OnGotFocus);
private void OnGotFocus(object sender, System.EventArgs e)
{
if (DataContext != null)
{
((SomeViewModelClass)DataContext).SetActiveTab();
}
}
So my problem is: when I select a tab for the first time, the OnGotFocus mehod is called, however when I select some other tabs, and come back and select it a second time, it doesn't get called for some reason, any one know why?
Thanks in advance!

You are using WPF's Tab Control Right?
Why are you doing this.GotFocus....? what is this exactly in this case?
You should do something like this if you want GetFocus to be called when a Tab is selected.
XAML (sample Tabs)
<TabControl>
<TabItem x:Name="table1"></TabItem>
<TabItem x:Name="table2"></TabItem>
<TabItem></TabItem>
</TabControl>
Code Behind
//register event for each individual tab
table1.GotFocus += new RoutedEventHandler(table1_GotFocus);
table2.GotFocus += new RoutedEventHandler(table2_GotFocus);
private void table1_GotFocus(object sender, RoutedEventArgs e)
{
}
private void table2_GotFocus(object sender, RoutedEventArgs e)
{
}
let me know if I misunderstood you requirement

Related

How to let MainWindow know that in Folder/Page2.xaml a Button is clicked

In my MainWindow.xaml i have a Frame. The content of this Frame will change when i click a Button. So if I click a button to show private customers, the Frame Content shows the site of the private customers:
private void privatecustomer_Click(object sender, RoutedEventArgs e)
{
Main.Content = new Privatecustomer.privatecustomer_show();
}
After clicking this Button the Frame will change the Content.
The new Content shows Privatecustomer.privatecustomer_show.xaml now.
In this xaml i have another Button. If i click this Button, the Content of the Frame in MainWindow should change to "Privatecustomer.privatecustomer_add.xaml".
But How I can tell from privatecustomer_show.xaml to MainWindow.xaml, that the Button addPrivatecustomer in privatecustomer_show is clicked and that the Main.Content have to change to
Main.Content = new Privatecustomer.privatecustomer_add();
?
I hope I can get some help here
How about adding an event in privatecustomer_show.xaml that the MainWindow.xaml can subscribe to.
Like this
public event EventHandler AddPrivateCustomer;
protected virtual void OnAddPrivateCustomerEventArgs e)
{
if (AddPrivateCustomer!= null)
AddPrivateCustomer(this, e);
}
Update: Please note the updated code, I made a copy'n'paste mistake in my first version.
Change your:
private void privatecustomer_Click(object sender, RoutedEventArgs e)
To:
private void privatecustomer_Click(object sender, RoutedEventArgs e)
{
var privateCustomerContent=new Privatecustomer.privatecustomer_show();
privateCustomerContent.AddPrivateCustomer+=onClick_addPrivateCustomer;
Main.Content = privateCustomerContent;
}
private onClick_addPrivateCustomer(object o,EventArgs e)
{
// Change Main.Content
}
The idea is that your privatecustomer_show control sends events for when it want to change something outside of what it has access to.
You MainWindow which has full control of all its child windows will then subscribe to each event.
Set Page2.Tag property to the instance of your MainWindow (use Binding in Xaml, or simply set it in code). After the specified Button in Page2 is clickd, simply use the Tag property (which is now your MainWindow).
Another option is to use
App.Current.MainWindow
When the Button is clicked. (A warning. I don't know the structure your project and this might not be the instance you are looking for).

If touch a control, how can I get this control name?

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);
}

Windows phone 8.1 textbox multiple click

If a textbox has focus and I want to be able to select it again is there a way to do this.
So first click the background turns blue and while it is still selected I press again the background turns green. How do I catch the second press even though its already selected?
You can subscribe to the PointerEntered and the SelectionChanged events. The first one is always fired when the pointer hits the TextBox. However if it contains text and you tap on it you will eventually select the text. The SelectionChanged handler will take care for that.
Your XAML markup looks as follows:
<TextBox x:Name="tb"
Text="Test"
PointerEntered="TextBox_PointerEntered"
SelectionChanged="TextBox_SelectionChanged"
GotFocus="TextBox_GotFocus"/>
The code behind file contains the following code:
private void TextBox_PointerEntered(object sender, PointerRoutedEventArgs e)
{
tb.Background = new SolidColorBrush(Colors.Green);
}
private void TextBox_SelectionChanged(object sender, RoutedEventArgs e)
{
tb.Background = new SolidColorBrush(Colors.Green);
}
private void TextBox_GotFocus(object sender, RoutedEventArgs e)
{
tb.Background = new SolidColorBrush(Colors.Blue);
}
You will have to adjust the code to your needs and take care of special cases where both SelectionChanged AND PointerEntered are fired (at this point both handlers do the same, so there's no problem).

Silverlight C# - Simulate a Click event on a button

I have an element with a Click method.
I would like to activate that method (or: fake a click on this element) within another function.
Is this possible?
No, there currently isnt a way to do this. See links:
https://stackoverflow.com/questions/4137528/ui-automation-for-wp7
windows phone 7: How to simulate control click programmatically
EDIT
This doesnt do exactly what you wanted to do, but you can do something like this and get the same outcome. I do this type of pattern alot in my code to do get the same outcome.
XAML
<Button Name="testClick" Click="testClick_Click">
<TextBlock Text="Click Me"></TextBlock>
</Button>
C#
private void testClick_Click(object sender, RoutedEventArgs e)
{
TestFunction(sender as Button);
}
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
//Call same function as button
TestFunction(testClick);
}
private void TestFunction(Button bt)
{
//do stuff
}
Somewhere in your class initiate a RoutedEventArgs:
private RoutedEventArgs evnt;
Then later call this:
element_Click(element, evnt);
That should do it.

How can I make an event for getting focus for double click on textbox? (Windows Phone)

I have a textBox and I call an event get focus, when click on it. The behaviour is different when I make a double click on it, how can I make an event for getting focus for double click on this textbox?
You can use the OnTap() and OnDoubleTap() methods of the TextBox. And in each method you can define the different logic and set the focus on the TextBox.
Update:
Here's a simple code structure on how to make it work:
XAML:
<TextBox x:Name="InputTextBox" Margin="0,0,0,520" />
C#
public MainPage()
{
InitializeComponent();
InputTextBox.Tap += InputTextBoxTap;
InputTextBox.DoubleTap += InputTextBoxDoubleTap;
}
private void InputTextBoxDoubleTap(object sender, System.Windows.Input.GestureEventArgs e)
{
InputTextBox.Text = "Double tapped!";
}
private void InputTextBoxTap(object sender, System.Windows.Input.GestureEventArgs e)
{
InputTextBox.Text = "Tapped!";
}
I tested this on both the emulator and on a device and it works in both cases!
The reason is simple.
If you have notice the arguments supplied are different
private void textBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
}
private void textBox1_Click(object sender, EventArgs e)
{
}
Yes if you want them to resemble the same you can select "MouseClick" event from the properties.
Cheers!

Categories