WPF-如何在用户控件中将两个不同的mousedown事件加载到主窗口?

时间:2016-06-09 11:26:05

标签: c# wpf xaml user-controls

如果你能帮助我,我会很高兴:) 我在usercontrol中有两个边框是名称UserControlMainScreen.And border有mousedown事件。

UserControlMainScreen.xaml:

<Grid Grid.Row="1" Name="Grid_MainButtons">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1*"></ColumnDefinition>
                <ColumnDefinition Width="2*"></ColumnDefinition>
                <ColumnDefinition Width="1*"></ColumnDefinition>
                <ColumnDefinition Width="2*"></ColumnDefinition>
                <ColumnDefinition Width="1*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Border Name="brd_BTES"  Grid.Column="1" MouseDown="UserControlMainScreen_brd_BTES_MouseDown" CornerRadius="10"  >
                <TextBlock Name="txtb_BTES" Text="BTES"></TextBlock>
             </Border>
            <Border Name="brd_SBE" Grid.Column="3" MouseDown="UserControlMainScreen_brd_SBE_MouseDown" CornerRadius="10" >
                <TextBlock Name="txtb_SBE" Text="SBE"></TextBlock>
            </Border>
</Grid>

问题:当我点击其中一个brd_SBE / brd_BTES时,我想要的是,应该发生不同的mousedown事件(如UserControlMainScreen_brd_SBE_MouseDown和UserControlMainScreen_brd_BTES_MouseDown)。但是当我单击其中一个边框时,事件发生在UserControlMainScreen_brd_SBE_MouseDown仅

MainWindow.cs

UserControlMainScreen uAnaEkran = new UserControlMainScreen();
        public MainWindow()
        {
            InitializeComponent();

            uAnaEkran.MouseDown += new MouseButtonEventHandler(UserControlMainScreen_brd_SBE_MouseDown);
            uAnaEkran.MouseDown += new MouseButtonEventHandler(UserControlMainScreen_brd_BTES_MouseDown);
        }
//some codes..

void UserControlMainScreen_brd_SBE_MouseDown(object sender, MouseButtonEventArgs e)
        {
            //if (sender == uAnaEkran.Grid_MainButtons.Children[1])

                brd_Menu2.Visibility = Visibility.Visible;
                brd_Menu1.Background = new ImageBrush(new BitmapImage(new Uri(BaseUriHelper.GetBaseUri(this), "Images/TopButton1Passive2.png")));
                brd_Menu2.Background = new ImageBrush(new BitmapImage(new Uri(BaseUriHelper.GetBaseUri(this), "Images/TopButton2Active2.png")));

                var bc = new BrushConverter();
                txtb_Menu1.Foreground = (Brush)bc.ConvertFrom("#FF0A2C62");
                txtb_Menu2.Text = uAnaEkran.txtb_SBE.Text.ToString() + " MODÜLÜ";

                e.Handled = true;

        }
        void UserControlMainScreen_brd_BTES_MouseDown(object sender, MouseButtonEventArgs e)
        {
            MessageBox.Show("btes");
        }

那么我该如何解决这个问题呢。 感谢。

1 个答案:

答案 0 :(得分:-1)

我解决了.Problem在MainWindow()。我将mouseDown添加到uAnaEkran.But我应该将mousedown添加到uAnaEkran.brd_SBE和uAnaEkran.brd_BTES.Answer如下:

uAnaEkran.brd_SBE.MouseDown += new MouseButtonEventHandler(UserControlMainScreen_brd_SBE_MouseDown);
uAnaEkran.brd_BTES.MouseDown += new MouseButtonEventHandler(UserControlMainScreen_brd_BTES_MouseDown);