如何在WPF中捕获工具栏项单击事件(How to catch toolbar item click event in WPF)

385 IT屋

First of all sorry if the answer is obvious but i'm new to WPF. Is there a better way to get the clicked button from toolbar? Every button is one of the drives so they are added programatically. The problem is that as soon i add an image to the buttons my code doesn't work any longer. I'm sure there is one easyer solution.

private void RightDriveToolBar_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
    int i = 0;
    if (e.LeftButton == MouseButtonState.Pressed)
    {
        string ButName = e.Source.ToString().Substring(32, 1);

        if (e.Source.ToString().Contains("System.Windows.Controls"))
        {
            foreach (var citem in RightDriveComboBox.Items)
            {
                if (citem.ToString().Substring(0, 1) == ButName)
                {
                    RightDriveComboBox.SelectedIndex = i;
                }
                i++;
            }
        }
    }
}


also the code for the buttons

Button But = new Button();
StackPanel ButStackPanel = new StackPanel();
Image ButImage = new Image();
TextBlock textBlock1 = new TextBlock();

ButImage.Source = new BitmapImage(new Uri("g:/Reinstall/Icons/105.png"));
ButImage.Width = 16;
textBlock1.TextAlignment = TextAlignment.Center;
textBlock1.Text = drive;

ButStackPanel.Orientation = Orientation.Horizontal;
ButStackPanel.Children.Add(ButImage);
ButStackPanel.Children.Add(textBlock1);

But.Content = ButStackPanel;
LeftDriveToolBar.Items.Add(But);
解决方案
If i understand you well, you have to add RoutedEventHandler[^].

Button Btn = new Button();
//set properties here
//...
//and the magic comes here:
Btn.Click += new RoutedEventHandler(ToolbarBtn_Click);

//and the subroutine which 
void ToolbarBtn_Click(object sender, RoutedEventArgs e)
{
    //your code
}


For further information, please see: WPF: A Beginner's guide: Part 3 of n[^]

首先对不起,如果答案很明显,但我是WPF的新手。有没有更好的方法从工具栏中获取单击按钮?每个按钮都是其中一个驱动器,因此它们以编程方式添加。问题是,只要我将图像添加到按钮,我的代码就不再起作用了。我确信有一个简易解决方案。



  private   void  RightDriveToolBar_PreviewMouseDown( object  sender,MouseButtonEventArgs e)
{
int i = 0 ;
if (e.LeftButton == MouseButtonState.Pressed)
{
string < / span> ButName = e.Source.ToString()。Substring( 32 1 );

if (e.Source.ToString()。包含(" < span class ="code-string"> System.Windows.Controls"
))
{
foreach var citem RightDriveComboBox.Items)
{
if (citem.ToString()。Substring( 0 1 )== ButName)
{
RightDriveComboBox.SelectedIndex = i;
}
i ++;
}
}
}
}




也是代码对于按钮



 Button But =  new  Button(); 
StackPanel ButStackPanel = new StackPanel();
Image ButImage = new Image();
TextBlock textBlock1 = new TextBlock();

ButImage.Source = new BitmapImage( new Uri(" g:/Reinstall/Icons/105.png"));
ButImage.Width = 16 ;
textBlock1.TextAlignment = TextAlignment.Center;
textBlock1.Text = drive;

ButStackPanel.Orientation = Orientation.Horizo​​ntal;
ButStackPanel.Children.Add(ButImage);
ButStackPanel.Children.Add(textBlock1);

But.Content = ButStackPanel;
LeftDriveToolBar.Items.Add(But);
解决方案
如果我理解你,你必须添加 RoutedEventHandler [ ^ ]。



按钮Btn = 按钮(); 
// 此处设置属性
// ...
// 魔术来到这里:
Btn.Click + = new RoutedEventHandler(ToolbarBtn_Click);

// 和子程序
void ToolbarBtn_Click( object sender,RoutedEventArgs e)
{
// 您的代码
}




如需了解更多信息,请参阅: WPF:初学者指南:n的第3部分 [ ^ ]

本文地址:IT屋 » 如何在WPF中捕获工具栏项单击事件