none
WPF TabControl双击切换选项卡的问题 RRS feed

  • 问题

  • 直接上代码:

    <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <TabControl Name="tabMain"  HorizontalAlignment="Left" Height="310" VerticalAlignment="Top" Width="507">
                <TabItem Header="TabItem" Name="tabItem1">
                    <Grid Background="#FFE5E5E5">
                        <Button Content="Button" HorizontalAlignment="Left" Margin="30,42,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click" MouseDoubleClick="Button_MouseDoubleClick"/>
                        <DataGrid HorizontalAlignment="Left" Margin="30,78,0,0" VerticalAlignment="Top" Height="163" Width="353" MouseDoubleClick="DataGrid_MouseDoubleClick"/>
                    </Grid>
                </TabItem>
                <TabItem Header="TabItem" Name="tabItem2">
                    <Grid Background="#FFE5E5E5">
                        <Button Content="Button" HorizontalAlignment="Left" Margin="47,41,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
                    </Grid>
                </TabItem>
            </TabControl>
    
        </Grid>
    </Window>

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfApplication1 { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Button_Click(object sender, RoutedEventArgs e) { //tabMain.SelectedIndex = 1; tabItem2.IsSelected = true; } private void Button_MouseDoubleClick(object sender, MouseButtonEventArgs e) { tabItem2.IsSelected = true; } private void DataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e) { //tabMain.SelectedIndex = 1;

    tabItem2.IsSelected = true; } private void Button_Click_1(object sender, RoutedEventArgs e) { tabItem1.IsSelected = true; } } }


    问题在双击DataGrid时执行切换选项卡的代码不起作用?单独在按钮中执行代码是有效地?看上去很不可思议,应该是隐藏着什么理论性的基础在里面,请高手指教


    • 已编辑 blwuer 2013年7月10日 6:44
    2013年7月10日 6:42

答案