如何通过单击 XAML WPF 中的标签来关注文本框
How to Focus on a textbox by Click on the label just in XAML WPF
这是我想要的:
当用户单击文本框旁边的标签时,我想专注于该文本框,我还必须将此事件“PreviewMouseLeftButtonUp”用于标签
我尝试了什么 :
<Window.Resources>
<Storyboard x:Key="OnLabelCLick">
<BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="FocusManager.FocusedElement" Storyboard.TargetName="text22">
<DiscreteBooleanKeyFrame KeyTime="0" Value="True"/>
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</Window.Resources>
<Window.Triggers>
<EventTrigger RoutedEvent="UIElement.PreviewMouseLeftButtonUp" SourceName="LBL1">
<BeginStoryboard Storyboard="{StaticResource OnLabelCLick}"/>
</EventTrigger>
</Window.Triggers>
<Grid>
<Label Content="Label" x:Name="LBL1" HorizontalAlignment="Left" Margin="210,200,0,0" VerticalAlignment="Top" Height="95" Width="370" Cursor="Hand" Background="#FF1B1B1C" Foreground="White"/>
<TextBox x:Name="text22" HorizontalAlignment="Left" Height="70" Margin="210,85,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="360" RenderTransformOrigin="0.5,0.5" HorizontalContentAlignment="Center" VerticalContentAlignment="Center">
</TextBox>
</Grid>
但是我看到这个错误
当我点击标签时:
System.InvalidOperationException: 'Cannot resolve all property references in the property path 'FocusManager.FocusedElement'. Verify that applicable objects support the properties.'
我只想在XAML
做所有事情
我该怎么做?
请帮忙
参考:Set focus on textbox in WPF
您可以添加活动 - LBL1_OnPreviewMouseLeftButtonDown
然后这样做:
<Label Content="Label"
x:Name="LBL1"
HorizontalAlignment="Left"
Margin="210,200,0,0"
VerticalAlignment="Top"
Height="95" Width="370"
Cursor="Hand"
Background="#FF1B1B1C"
Foreground="White"
PreviewMouseLeftButtonUp="LBL1_OnPreviewMouseLeftButtonUp"
PreviewMouseLeftButtonDown="LBL1_OnPreviewMouseLeftButtonDown"/>
private void LBL1_OnPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
text22.Focus();
}
您可以为此使用附加属性
using System.Windows;
using System.Windows.Controls;
namespace WpfApp4.Extensions // may be different in your application!
{
public class FocusHandling : DependencyObject
{
public static Control GetFocusControl(Label obj)
{
return (Control)obj.GetValue(FocusControlProperty);
}
public static void SetFocusControl(Label obj, Control value)
{
obj.SetValue(FocusControlProperty, value);
}
// Using a DependencyProperty as the backing store for FocusControl. This enables animation, styling, binding, etc...
public static readonly DependencyProperty FocusControlProperty =
DependencyProperty.RegisterAttached("FocusControl", typeof(Control), typeof(FocusHandling), new PropertyMetadata(null, FocusControlChanged));
private static void FocusControlChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is Label label)
{
if (e.OldValue is not null && e.NewValue is null)
label.MouseLeftButtonUp -= Label_MouseLeftButtonUp;
if (e.OldValue is null && e.NewValue is not null)
label.MouseLeftButtonUp += Label_MouseLeftButtonUp;
}
}
private static void Label_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
if (sender is Label label)
{
var focusControl = GetFocusControl(label);
if (focusControl.Focusable)
focusControl.Focus();
}
}
}
}
在你的 XAML
<Window
...
xmlns:extensions="clr-namespace:WpfApp4.Extensions" <!-- may be different in your application -->
...>
<Grid>
<Label
extensions:FocusHandling.FocusControl="{x:Reference text22}"
x:Name="LBL1"
Width="370"
Height="95"
Margin="210,200,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Background="#FF1B1B1C"
Content="Label"
Cursor="Hand"
Foreground="White" />
<TextBox
x:Name="text22"
Width="360"
Height="70"
Margin="210,85,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
RenderTransformOrigin="0.5,0.5"
Text="TextBox"
TextWrapping="Wrap" />
</Grid>
</Window>
这是我想要的: 当用户单击文本框旁边的标签时,我想专注于该文本框,我还必须将此事件“PreviewMouseLeftButtonUp”用于标签
我尝试了什么 :
<Window.Resources>
<Storyboard x:Key="OnLabelCLick">
<BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="FocusManager.FocusedElement" Storyboard.TargetName="text22">
<DiscreteBooleanKeyFrame KeyTime="0" Value="True"/>
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</Window.Resources>
<Window.Triggers>
<EventTrigger RoutedEvent="UIElement.PreviewMouseLeftButtonUp" SourceName="LBL1">
<BeginStoryboard Storyboard="{StaticResource OnLabelCLick}"/>
</EventTrigger>
</Window.Triggers>
<Grid>
<Label Content="Label" x:Name="LBL1" HorizontalAlignment="Left" Margin="210,200,0,0" VerticalAlignment="Top" Height="95" Width="370" Cursor="Hand" Background="#FF1B1B1C" Foreground="White"/>
<TextBox x:Name="text22" HorizontalAlignment="Left" Height="70" Margin="210,85,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="360" RenderTransformOrigin="0.5,0.5" HorizontalContentAlignment="Center" VerticalContentAlignment="Center">
</TextBox>
</Grid>
但是我看到这个错误
当我点击标签时:
System.InvalidOperationException: 'Cannot resolve all property references in the property path 'FocusManager.FocusedElement'. Verify that applicable objects support the properties.'
我只想在XAML
做所有事情我该怎么做?
请帮忙
参考:Set focus on textbox in WPF
您可以添加活动 - LBL1_OnPreviewMouseLeftButtonDown
然后这样做:
<Label Content="Label"
x:Name="LBL1"
HorizontalAlignment="Left"
Margin="210,200,0,0"
VerticalAlignment="Top"
Height="95" Width="370"
Cursor="Hand"
Background="#FF1B1B1C"
Foreground="White"
PreviewMouseLeftButtonUp="LBL1_OnPreviewMouseLeftButtonUp"
PreviewMouseLeftButtonDown="LBL1_OnPreviewMouseLeftButtonDown"/>
private void LBL1_OnPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
text22.Focus();
}
您可以为此使用附加属性
using System.Windows;
using System.Windows.Controls;
namespace WpfApp4.Extensions // may be different in your application!
{
public class FocusHandling : DependencyObject
{
public static Control GetFocusControl(Label obj)
{
return (Control)obj.GetValue(FocusControlProperty);
}
public static void SetFocusControl(Label obj, Control value)
{
obj.SetValue(FocusControlProperty, value);
}
// Using a DependencyProperty as the backing store for FocusControl. This enables animation, styling, binding, etc...
public static readonly DependencyProperty FocusControlProperty =
DependencyProperty.RegisterAttached("FocusControl", typeof(Control), typeof(FocusHandling), new PropertyMetadata(null, FocusControlChanged));
private static void FocusControlChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is Label label)
{
if (e.OldValue is not null && e.NewValue is null)
label.MouseLeftButtonUp -= Label_MouseLeftButtonUp;
if (e.OldValue is null && e.NewValue is not null)
label.MouseLeftButtonUp += Label_MouseLeftButtonUp;
}
}
private static void Label_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
if (sender is Label label)
{
var focusControl = GetFocusControl(label);
if (focusControl.Focusable)
focusControl.Focus();
}
}
}
}
在你的 XAML
<Window
...
xmlns:extensions="clr-namespace:WpfApp4.Extensions" <!-- may be different in your application -->
...>
<Grid>
<Label
extensions:FocusHandling.FocusControl="{x:Reference text22}"
x:Name="LBL1"
Width="370"
Height="95"
Margin="210,200,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Background="#FF1B1B1C"
Content="Label"
Cursor="Hand"
Foreground="White" />
<TextBox
x:Name="text22"
Width="360"
Height="70"
Margin="210,85,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
RenderTransformOrigin="0.5,0.5"
Text="TextBox"
TextWrapping="Wrap" />
</Grid>
</Window>