以编程方式将焦点设置为 WPF 选项卡项
本文关键字:WPF 选项 设置 编程 方式 焦点 | 更新日期: 2023-09-27 18:30:52
我是wpf的新手。我正在开发一个新应用程序。在我的应用程序中,我使用选项卡控件。使用两个选项卡项。在一个选项卡项中,数据网格用于显示。在单击数据网格行时,我想将焦点设置为下一个选项卡项。
在Row_DoubleClick上,代码如下...
if (dgMain.SelectedItem == null)
return;
cardType = ((Entity.CardType)dgMain.SelectedItem);
txtCardType.Text = cardType.card_type_desc;
tabMain.SelectedItem = tbiConfig;
此代码不起作用。焦点未移动到另一个选项卡。
伙计们,你能帮我吗?
------ XAML 如下所示
<Grid>
<Label Content="Card Type" Height="35" HorizontalAlignment="Left" Margin="17,12,0,0" Name="lblHeader" VerticalAlignment="Top" FontSize="18" Width="193" />
<TabControl Height="235" HorizontalAlignment="Left" Margin="12,53,0,0" Name="tabMain" VerticalAlignment="Top" Width="576">
<TabItem Header="Configure" Name="tbiConfig">
<Grid>
<Button Content="Save" Height="23" HorizontalAlignment="Left" Margin="450,137,0,0" Name="btnSave" VerticalAlignment="Top" Width="75" Click="btnSave_Click" />
<Label Content="CardType" Height="28" HorizontalAlignment="Left" Margin="14,34,0,0" Name="lblCardType" VerticalAlignment="Top" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="130,36,0,0" Name="txtCardType" VerticalAlignment="Top" Width="395" />
</Grid>
</TabItem>
<TabItem Header="Search" Name="tbiSearch">
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Label Content="CardType" Height="28" HorizontalAlignment="Left" Margin="10,10,0,0" Name="lblSearchCardType" VerticalAlignment="Top" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="126,12,0,0" Name="txtSearchCardType" VerticalAlignment="Top" Width="333" />
<Button Content="Search" Height="23" HorizontalAlignment="Left" Margin="473,12,0,0" Name="btnSearch" VerticalAlignment="Top" Width="75" Click="btnSearch_Click" />
<DataGrid AutoGenerateColumns="false" Height="148" HorizontalAlignment="Left" Margin="15,49,0,6" Name="dgMain" VerticalAlignment="Center" Width="533" ItemsSource="{Binding}" CanUserAddRows="True" GridLinesVisibility="All" HorizontalGridLinesBrush="LightGray" IsEnabled="True" IsReadOnly="True" RowHeight="23" HorizontalContentAlignment="Left" VerticalGridLinesBrush="LightGray" VerticalContentAlignment="Center" MouseDoubleClick="dgMain_MouseDoubleClick">
<DataGrid.ItemContainerStyle>
<Style TargetType="DataGridRow">
<EventSetter Event="MouseDoubleClick" Handler="Row_DoubleClick"/>
</Style>
</DataGrid.ItemContainerStyle>
<DataGrid.Columns >
<DataGridTextColumn Header="ID" Binding="{Binding Path=card_type_id}" Width="100" />
<DataGridTextColumn Header="Description" Binding="{Binding Path=card_type_desc}" Width="400" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</TabItem>
</TabControl>
</Grid>
我意识到这很旧,也许.Focus() 在 2013 年不可用,但现在您可以使用:
tbiConfig.Focus()
尝试:
int tbiConfig = 1;
tabMain.SelectedIndex = tbiConfig;
最好在foreach
循环中使用选定项
foreach(TabItem tbiConfig in tabMain)
{
tabMain.SelectedItem = tbiConfig;
}