Reputation: 3366
As hard as I try I can not find how to reference my tag in my CS file.
In my XAML file I have the following
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<TextBlock Text="User Id" Margin="30" Foreground="White" FontSize="25" />
<TextBox x:Name="logInUserIdText" IsSpellCheckEnabled="True" Height="40" Margin="13,1" Width="408" InputScope="EmailSmtpAddress" FontFamily="Global User Interface" KeyDown="logInUserIdKeyDown" Tag="1" />
</StackPanel>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<TextBlock Text="Password" Margin="20" Foreground="White" FontSize="25" />
<PasswordBox x:Name="logInPasswordText" IsPasswordRevealButtonEnabled="True" Height="40" Margin="5,1" Width="408" KeyDown="logInPasswordKeyDown" Tag="2"/>
</StackPanel>
Now in my CS file I have the following
private void logInUserIdKeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == VirtualKey.Enter)
{
logInUserIdString = logInUserIdText.Text;
Debug.WriteLine("aa");
logInPasswordText.Focus(FocusState.Programmatic);
}
}
private void logInPasswordKeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == VirtualKey.Enter)
{
logInPasswordString = logInPasswordText.Password;
Debug.WriteLine("bb");
}
}
And the issue is that regardless of which box is in focus, if I hit ENTER on the keyboard both methods get fired and the output is aa bb which are the debug statements in each method.
So I assume that since I set the tag value in XAML I should be able to do something like
if (e.Key == Virtual.Enter)&&(e.Tag == 1)
Debug.WriteLine("loginuserid method only called");
But I can not, it wont let me reference the tag in the CS file. Why not?
Any help would be much appreciated.
Upvotes: 1
Views: 68
Reputation: 89285
What happen to you seems impossible. To inspect more closely, try to run your application in debug mode, put breakpoint at the beginning of both method. Then, when execution hit breakpoint, you can see which control trigger that event from sender
parameter (I prefer to use visual studio watch window to see content of variable).
The same concept can be used to get Tag
value :
var s = (FrameworkElement)sender;
var tagValue = (string)s.Tag;
But I suggest to fix the original problem rather than workaround it with using Tag
value.
Upvotes: 2