[英]Set focus to a button in a new TabItem
I use a TabControl to display a list of items. 我使用TabControl来显示项目列表。 The ItemView is a separate control I wrote. ItemView是我编写的单独控件。
<TabControl SelectedItem="{Binding CurrentItem}"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding ItemList}">
<TabControl.ContentTemplate>
<DataTemplate>
<ctrls:ItemView/>
</DataTemplate>
</TabControl.ContentTemplate>
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ShortItemDescription}"/>
</DataTemplate>
</TabControl.ItemTemplate>
</TabControl>
If the user presses a Button a new ViewModel is added to the list of ViewModels and the TabControl displays it as a new tab. 如果用户按下Button,则将新的ViewModel添加到ViewModels列表中,并且TabControl将其显示为新的选项卡。 After adding the new tab is selected. 添加后,新选项卡被选中。
<Button Command="{Binding AddItemCommand}"
Content="Add item"/>
Inside of the new View is a button that needs to be focused each time a new tab is added. 新视图的内部是一个按钮,每次添加新选项卡时都需要将其聚焦。 I have tried to use the FocusManager and the Initialized event in the ItemView but these are only called for the first time I add a new tab. 我试图在ItemView中使用FocusManager和Initialized事件,但是只有在我第一次添加新标签时才调用它们。
<UserControl x:Class="ItemView"
...
Initialized="ViewInitialized">
<Grid>
...
<!-- Set focus to this button -->
<Button Content="Search"
Command="{Binding SearchCommand}"
Name="SearchButton"
Grid.Column="0"
Grid.Row="0"/>
</Grid>
</UserControl>
Any ideas? 有任何想法吗?
Update 更新资料
The content of the Initialize method in the ItemView is currently this one - which is working fine... for the first time and then never again 当前,ItemView中Initialize方法的内容是这个-运作良好... 第一次,然后再也没有
public delegate void ChangeFocusDelegate();
private void FocusSearchButton()
{
SearchButton.Focus();
}
private void ViewInitialized(object sender, EventArgs e)
{
ChangeFocusDelegate focusDelegate = FocusSearchButton;
Dispatcher.BeginInvoke(DispatcherPriority.ContextIdle, focusDelegate);
}
This should be work like this (over your defined Initialized event of the UserControl): 这应该是这样的(在您定义的UserControl的Initialized事件上):
private void ViewInitialized(object sender, System.EventArgs e)
{
this.SearchButton.Focus();
}
您需要在视图上预订Loaded事件,并在事件处理程序方法中调用按钮上的Focus()。
It seemed to bit a bit strange... 似乎有点奇怪...
IsVisibleChanged ... no reaction IsVisibleChanged ...无反应
Initialized ... no reaction 已初始化...无反应
Loaded ... no reaction 已加载...无反应
And then I got the solution. 然后我得到了解决方案。 What if the runtime uses the same View for each tab and changes only the data inside to another ViewModel. 如果运行时每个选项卡使用相同的View并将仅内部的数据更改为另一个ViewModel,该怎么办。 So the solution is: 所以解决方案是:
private void SameViewButDataContextChanged(object sender, System.Windows.DependencyPropertyChangedEventArgs e)
{
ChangeFocusDelegate focusDelegate = FocusFileSearchButton;
Dispatcher.BeginInvoke(DispatcherPriority.ContextIdle, focusDelegate);
}
And each time I add a tab the focus is switched to the Search button. 每次我添加一个标签时,焦点便切换到“搜索”按钮。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.