Toolbar is an ItemsControl and does not have click event handler to indicate which button is clicked. Here Hit Testing is used to figure that out:
Note that we need to use Preview to Tunneling Event Handler
void tbr_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
ToolBar tbr = sender as ToolBar;
Point pt=e.GetPosition(tbr);
object btnClicked=null;
UIElement element = tbr.InputHitTest(pt) as UIElement;
while (element != null)
{
if (element == tbr) return;
btnClicked = tbr.ItemContainerGenerator.ItemFromContainer(element);
if (!btnClicked.Equals(DependencyProperty.UnsetValue)) break;
element = (UIElement)VisualTreeHelper.GetParent(element);
}
if (btnClicked != null)
Console.WriteLine((btnClicked as Button).Content);
else
Console.WriteLine("no item found");
}
Sunday, September 9, 2012
Hit Test Toolbar to find button
Subscribe to:
Comments (Atom)