這就是我想要的:當用戶單擊文本框旁邊的標簽時,我想專注于該文本框,我還必須使用此事件“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:'無法決議屬性路徑'FocusManager.FocusedElement'中的所有屬性參考。驗證適用的物件是否支持這些屬性。
我只想在 XAML 中做所有事情
我怎樣才能做到這一點 ?
請幫忙
參考:在 WPF 中的文本框上設定焦點
uj5u.com熱心網友回復:
您可以為此使用 AttachedProperty
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>
uj5u.com熱心網友回復:
您可以添加事件 - 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();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/424239.html
