当我在窗体中,放入TabControl(里面有TabItem1和TabItem2),再放一个Button。Button的Clicked事件中,改变TabItem2的IsSelected属性为True。这时,按下按钮,TabItem2显示。------------------------------当我在TabItem1中,放入一个ComboBox后,再按Button,TabItem2则不会显示。我知道这一点是WPF路由事件惹的祸,请问大家如何解决该问题?

解决方案 »

  1.   


    <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 Height="181" HorizontalAlignment="Left" Margin="63,105,0,0" Name="tabControl1" VerticalAlignment="Top" Width="372" SelectionChanged="tabControl1_SelectionChanged">
                <TabItem Header="tabItem1" Name="tabItem1">
                    <Grid>
                        <ComboBox Height="23" HorizontalAlignment="Left" Margin="183,64,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" SelectionChanged="comboBox1_SelectionChanged" />
                    </Grid>
                </TabItem>
                <TabItem Header="tabItem2" Name="tabItem2">
                    <Grid />
                </TabItem>
            </TabControl>
            <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="150,46,0,0" Name="button1" VerticalAlignment="Top" Width="75" />
        </Grid>
    </Window>
      

  2.   


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    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 button1_Click(object sender, RoutedEventArgs e)
            {
                tabItem2.IsSelected = true;
            }        private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {        }        private void tabControl1_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {        }
        }
    }
      

  3.   

    <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="150,46,0,0" Name="button1" VerticalAlignment="Top" Width="75" />Click 事件呢? 你没指定啊Click="button1_Click"