Adding IInvokeProvider into a Custom ToggleButton  
Author Message
Philippe Galipeau





PostPosted: Microsoft UI Automation, Adding IInvokeProvider into a Custom ToggleButton Top

How I can implement IInvokeProvider into an

custom ToggleButton. Do you have some exemple

Thanks.



Software Development for Windows Vista16  
 
 
Philippe Galipeau





PostPosted: Microsoft UI Automation, Adding IInvokeProvider into a Custom ToggleButton Top

I found finaly the correct aswer.


using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Controls.Primitives;
using System.Windows;
using System.Windows.Automation.Provider;
using System.Windows.Automation;
using System.Windows.Input;
using System.Windows.Automation.Peers;

namespace Test
{
public class MyButton : ToggleButton
{
public MyButton()
{
Click += new RoutedEventHandler(MyButton_Click);
}

void MyButton_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Click");
}

#region Automation
protected override AutomationPeer OnCreateAutomationPeer()
{
return new MyButtonAutomationPeer(this);
}
#endregion
}

public class MyButtonAutomationPeer : FrameworkElementAutomationPeer, IInvokeProvider, IToggleProvider
{
public MyButtonAutomationPeer(MyButton control)
: base(control)
{
}

protected override string GetClassNameCore()
{
return "MyButton";
}

protected override AutomationControlType GetAutomationControlTypeCore()
{
return AutomationControlType.Button;
}

public override object GetPattern(PatternInterface patternInterface)
{
if (patternInterface == PatternInterface.Invoke)
{
return this;
}
if (patternInterface == PatternInterface.Toggle)
{
return this;
}
return base.GetPattern(patternInterface);
}


private MyButton MyOwner
{
get
{
return (MyButton)base.Owner;
}
}

#region IInvokeProvider Members

public void Invoke()
{
RoutedEventArgs newEventArgs = new RoutedEventArgs(ButtonBase.ClickEvent);
MyOwner.RaiseEvent(newEventArgs);
}

#endregion

#region IToggleProvider Members

public void Toggle()
{
MyOwner.IsChecked = !MyOwner.IsChecked;
}

public ToggleState ToggleState
{
get { return ((bool)MyOwner.IsChecked ToggleState.On : ToggleState.Off); }
}

#endregion
}
}