Vac*_*ano 25 c# wpf focus eventtrigger mvvm
有没有办法Focus
使用WPF从一个控件设置到另一个控件Trigger
?
如下例所示:
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBox Name="txtName"></TextBox>
<TextBox Grid.Row="1" Name="txtAddress"></TextBox>
<Button Grid.Row="2" Content="Finish">
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<!-- Insert cool code here-->
</EventTrigger>
</Button.Triggers>
</Button>
</Grid>
</Page>
Run Code Online (Sandbox Code Playgroud)
有没有办法EventTrigger
把重点放在textBox"txtName"上?
我试图找到使用严格的MVVM做这样的事情的方法.如果这是不应该通过XAML(在MVVM中)完成的,那么这很好.但我希望看到一些文档,说明它如何适合在XAML之外的MVVM模式.
Ian*_*kes 32
您是否考虑过使用附加行为?它们易于实现并使用AttachedProperty.虽然它仍然需要代码,但是这个代码在类中被抽象出来并被重用.它们可以消除"后面的代码"的需要,并且经常与MVVM模式一起使用.
试试这个,看看它是否适合你.
public class EventFocusAttachment
{
public static Control GetElementToFocus(Button button)
{
return (Control)button.GetValue(ElementToFocusProperty);
}
public static void SetElementToFocus(Button button, Control value)
{
button.SetValue(ElementToFocusProperty, value);
}
public static readonly DependencyProperty ElementToFocusProperty =
DependencyProperty.RegisterAttached("ElementToFocus", typeof(Control),
typeof(EventFocusAttachment), new UIPropertyMetadata(null, ElementToFocusPropertyChanged));
public static void ElementToFocusPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
var button = sender as Button;
if (button != null)
{
button.Click += (s, args) =>
{
Control control = GetElementToFocus(button);
if (control != null)
{
control.Focus();
}
};
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后在你的XAML做类似......
<Button
Content="Click Me!"
local:EventFocusAttachment.ElementToFocus="{Binding ElementName=textBox}"
/>
<TextBox x:Name="textBox" />
Run Code Online (Sandbox Code Playgroud)
cae*_*say 16
我不在视觉工作室附近所以我现在不能尝试这个,但是在我的头脑中,你应该能够做到这样的事情:
FocusManager.FocusedElement="{Binding ElementName=txtName}">
Run Code Online (Sandbox Code Playgroud)
编辑:
这里有一个关于此的后续问题(最近询问):如何仅在xaml中设置自动对焦?其中包含此方法,以及如何使用它的一些不同的想法.
dro*_*ter 10
你也可以使用WPF行为......
public class FocusElementAfterClickBehavior : Behavior<ButtonBase>
{
private ButtonBase _AssociatedButton;
protected override void OnAttached()
{
_AssociatedButton = AssociatedObject;
_AssociatedButton.Click += AssociatedButtonClick;
}
protected override void OnDetaching()
{
_AssociatedButton.Click -= AssociatedButtonClick;
}
void AssociatedButtonClick(object sender, RoutedEventArgs e)
{
Keyboard.Focus(FocusElement);
}
public Control FocusElement
{
get { return (Control)GetValue(FocusElementProperty); }
set { SetValue(FocusElementProperty, value); }
}
// Using a DependencyProperty as the backing store for FocusElement. This enables animation, styling, binding, etc...
public static readonly DependencyProperty FocusElementProperty =
DependencyProperty.Register("FocusElement", typeof(Control), typeof(FocusElementAfterClickBehavior), new UIPropertyMetadata());
}
Run Code Online (Sandbox Code Playgroud)
以下是使用该行为的XAML.
包含名称空间:
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:local="clr-namespace:WpfApplication1"
Run Code Online (Sandbox Code Playgroud)
将WPF行为附加到按钮并将要设置焦点的元素绑定到:
<Button Content="Focus" Width="75">
<i:Interaction.Behaviors>
<local:FocusElementAfterClickBehavior FocusElement="{Binding ElementName=CheckBoxComboBox, Mode=OneWay}"/>
</i:Interaction.Behaviors>
</Button>
<ComboBox x:Name="CheckBoxComboBox" HorizontalAlignment="Center" VerticalAlignment="Center" Width="120" Grid.Row="1"/>
Run Code Online (Sandbox Code Playgroud)
所以这样你就没有代码了,它可以重用于从ButtonBase继承的任何控件.
希望这有助于某人.