In WPF, a button and a click event is:
<Button Click="Btn_Click" />
private void Btn_Click(object sender, RoutedEventArgs e)
{
....
}
In DevExpress, according to the documentation, I would guess the equivalent would be:
<dxb:BarButtonItem ItemClick="Btn_Click" />
private event ItemClickEventHandler Btn_Click(Item sender, RoutedEvent e)
{
....
}
But it is not compiling. I would guess it is very easy but I cannot find any example in the documentation.
The second parameter of the handler should be a ItemClickEventArgs:
private void Btn_Click(object sender, ItemClickEventArgs e)
{
....
}
Related
I am working in Silverlight 5 webbrowser control to show the Contents. I have Used this Code Which Worked Fine For me.
Xaml:
<TextBox x:Name="txtUri" Margin="5" HorizontalAlignment="Stretch" />
<Button x:Name="btnGo" Grid.Column="1" Content="Go" Click="OnGo" Margin="5" />
<WebBrowser LoadCompleted="browserControl_LoadCompleted" ScriptNotify="browserControl_ScriptNotify" Grid.Row="1" Margin="5" x:Name="browserControl" />
.CS
private void webBrowser_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
MessageBox.Show("called");
}
private void browserControl_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
MessageBox.Show("called");
btnGo.IsEnabled = true;
}
private void OnGo(object sender, RoutedEventArgs e)
{
btnGo.IsEnabled = false;
browserControl.Navigate(new Uri(txtUri.Text, UriKind.RelativeOrAbsolute));
}
private void browserControl_ScriptNotify(object sender, NotifyEventArgs e)
{
if (!string.IsNullOrEmpty(e.Value))
{
MessageBox.Show(e.Value);
}
}
It Disable the Button And Search For TextBox entered URI but When i change the OnGo Method
private void OnGo(object sender, RoutedEventArgs e)
{
btnGo.IsEnabled = false;
browserControl.NavigateToString("<html> <head><title>Sample Readme File</title><script type='text/javascript' language='javascript'> function GetSelectedText() {window.external.notify('Text to show in Message box');}</script></head> <body> <a onclick='GetSelectedText()'>Click Me</a> <p>Sample Readme Content</p> </body></html>");
}
And Add the ScriptNotify Event Handler
void webBrowser_ScriptNotify(object sender, NotifyEventArgs e)
{
if (!string.IsNullOrEmpty(e.Value))
{
MessageBox.Show(e.Value);
}
}
Then webBrowser_LoadCompleted event does not Triggered.
What is the difference behind these two.
Please Help!
I am trying out custom routed events, but I get a TargetInvocationException when compiling with an Attached Event Handler.
I have the following code inside custom control EventRaiserControl:
public static readonly RoutedEvent KickedEvent = EventManager.RegisterRoutedEvent("KickedEvent", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(EventRaiserControl));
public event RoutedEventHandler Kicked
{
add
{ this.AddHandler(KickedEvent, value); }
remove
{ this.RemoveHandler(KickedEvent, value); }
}
private void btn1_Click(object sender, RoutedEventArgs e)
{
RaiseEvent(new RoutedEventArgs(KickedEvent));
}
I then have the following XAML in my main window:
<StackPanel local:EventRaiserControl.Kicked="StackPanel_Kicked">
<local:EventRaiserControl Kicked="EventRaiserControl_Kicked"/>
</StackPanel>
With the following handlers in the MainWindow code behind:
private void StackPanel_Kicked(object sender, RoutedEventArgs e)
{
Console.WriteLine("Caught Kicked Event at Panel level.");
}
private void EventRaiserControl_Kicked(object sender, RoutedEventArgs e)
{
Console.WriteLine("Caught Kicked Event at Control level.");
}
My code works fine with this handler:
<local:EventRaiserControl Kicked="EventRaiserControl_Kicked"/>
But fails with TargetInvocationException the moment I add the attached handler:
<StackPanel local:EventRaiserControl.Kicked="StackPanel_Kicked">
Can somebody help? What am I missing / misusing?
Many thanks
At first I was surprised why it is, but I saw the reason after coding as your code. Just change
EventManager.RegisterRoutedEvent("KickedEvent"....
to
EventManager.RegisterRoutedEvent("Kicked"....
What is the equivalent Following code in wpf
code in winapp :
private void textBox1_Enter(object sender, EventArgs e)
{
MessageBox.Show("www.stackoverflow.com");
}
There is no Enter event on a WPF textbox - you could use the GotFocus event to the same effect though.
private void textbox1_GotFocus(object sender, System.Windows.RoutedEventArgs e)
{
MessageBox.Show("www.stackoverflow.com");
}
this is accessed in the XAML as follows:
<TextBox GotFocus="textbox1_GotFocus"/>
What is the equivalent Following code in wpf
code in winapp :
private void textBox1_Leave(object sender, EventArgs e)
{
MessageBox.Show("www.stackoverflow.com");
}
private void textBox1_LostFocus(object sender, RoutedEventArgs e)
{
MessageBox.Show("www.stackoverflow.com");
}
(actually you could even reuse the same signature as before, all you need to do is hook the method to the LostFocus event instead of the Leave event)
I have a problem with bubbeling events. I manage to bubble events in borders, grid, stackpanel, but not in a ScrollViewer
If you look at the example below you will notice that when you click the TextBlock the event is bubbeled up to the Grid. But when I include the ScrollViewer the event stops here and is not sent up to the Grid.
Does anyone now whay this happends and if it can be fixed? I really need to be able to bubble events through a ScrollViewer as I use it all the time.
<Grid MouseLeftButtonDown="Grid_MouseLeftButtonDown">
<!--<ScrollViewer MouseLeftButtonDown="ScrollViewer_MouseLeftButtonDown">-->
<StackPanel Orientation="Vertical" MouseLeftButtonDown="StackPanel_MouseLeftButtonDown">
<TextBlock Text="Click me to bubble an event" MouseLeftButtonDown="TextBlock_MouseLeftButtonDown"/>
</StackPanel>
<!--</ScrollViewer>-->
</Grid>
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("LayoutRoot clicked");
}
private void ScrollViewer_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("ScrollViewer clicked");
e.Handled = false;
}
private void StackPanel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("StackPanel clicked");
e.Handled = false;
}
private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("Textblock clicked");
e.Handled = false;
}
}
use AddHandler(yourDelegate, True); syntax for adding event handlers, which will ignore Handled flag set by other controls in the visual tree.
I had this problem and the fix posted by user572559 fixed my issue. For those that need it, below is what I did (modified for posting):
_scrollViewer = new ScrollViewer();
_scrollViewer.AddHandler(
ScrollViewer.MouseLeftButtonDownEvent,
new MouseButtonEventHandler(OnMouseLeftButtonDown),
true);
_scrollViewer.AddHandler(
ScrollViewer.MouseLeftButtonUpEvent,
new MouseButtonEventHandler(OnMouseLeftButtonUp),
true);
...
void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
...
}
void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
...
}
Also note that if you hare handling these you may be handling MouseMove as well. MouseMove worked for me without needing to do this, and it also does not seem to be supported in this fashion (not a bubbling event).
You can prevent e.Handled on MouseButtonEventArgs by overriding ScrollViewer like this
public sealed class ClickScrollViewer : ScrollViewer
{
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
this.Focus();
}
}