【问题标题】:WPF-How to load two different mousedown event in user control to mainwindow?WPF-如何将用户控件中的两个不同 mousedown 事件加载到主窗口?
【发布时间】:2016-06-09 11:26:04
【问题描述】:

如果你能帮助我,我会很高兴:) 我在用户控件中有两个边框,名称为 UserControlMainScreen。并且边框有 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");
        }

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

【问题讨论】:

  • 请让您的问题更清楚。
  • @ViVi 谢谢你的回复。你能再检查一下吗?
  • 事实上,我想在 UserControlMainScreen_brd_SBE_MouseDown 函数 @ViVi 中控制点击的边框名称/Uid

标签: c# wpf xaml user-controls


【解决方案1】:

我解决了。问题在 MainWindow() 中。我将 MouseDown 添加到 uAnaEkran。但我应该将 mousedown 添加到 uAnaEkran.brd_SBE 和 uAnaEkran.brd_BTES。答案如下:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-17
    • 1970-01-01
    • 2014-12-28
    • 2021-11-12
    相关资源
    最近更新 更多