以编程方式将焦点设置为WPF选项卡项

时间:2013-09-10 15:27:30

标签: c# wpf tabcontrol

我是wpf的新手。我正在开发一个新的应用程序。在我的应用程序中,我使用选项卡控件。使用了两个标签项。在一个选项卡项中,数据网格用于显示。 单击datagrid行时,我想将焦点设置为下一个标签项。

在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>

2 个答案:

答案 0 :(得分:0)

尝试:

int tbiConfig = 1;
tabMain.SelectedIndex = tbiConfig;

使用SelectedItem最适合foreach循环

foreach(TabItem tbiConfig in tabMain)
{
    tabMain.SelectedItem = tbiConfig;
}

答案 1 :(得分:0)

我意识到这很古老,也许。Focus()在2013年不可用,但现在您可以使用:

tbiConfig.Focus()