WPF DataContext syntax - Convert from C# to VB - wpf

Currently working through a Teach Yourself WPF tutorial. Usually I can mentally convert from C# to VB without any problem but this C# syntax is unfamiliar. How is it written in VB?
Private void Button_Click(object sender, RoutedEventArgs e)
{
((Person)DataContext).FirstName = "blah blah"
}
My usual fave online converters are choking on this ... perhaps because they don't do WPF?

try this
Private Sub Button_Click(ByVal sender As System.Object, ByVal e as RoutedEventArgs) Handles Button.Click
DirectCast(DataContext, Person).FirstName = "blah blah"
End Sub

Related

Disable Copy/Paste/Cut in WPF VB.NET

I am trying to disable Copy/Paste/Cut feature in textbox in WPF VB.NET
This code works fine :
Private Sub textbox_PreviewExecuted(ByVal sender As Object, ByVal e As ExecutedRoutedEventArgs)
If e.Command Is ApplicationCommands.Copy OrElse e.Command Is ApplicationCommands.Cut OrElse e.Command Is ApplicationCommands.Paste Then
e.Handled = True
End If
End Sub
But disabling CTRL+C/CTRL+V using this code doesn't work :
DataObject.AddPastingHandler(control, AddressOf Me.OnCancelCommand)
DataObject.AddCopyingHandler(control, AddressOf Me.OnCancelCommand)
private void OnCancelCommand(Object sender, DataObjectEventArgs e)
e.CancelCommand()
How to make the 2nd code work ?
You can re-bind the commands. Here is a C# version, VB.Net should be similar
<TextBox>
<TextBox.CommandBindings>
<CommandBinding Command="ApplicationCommands.Copy" CanExecute="CommandBinding_CanExecute"/>
<CommandBinding Command="ApplicationCommands.Paste" CanExecute="CommandBinding_CanExecute"/>
</TextBox.CommandBindings>
</TextBox>
Handler:
private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = false;
e.Handled = true;
}
Consider adding ApplicationCommands.Cut to the party, unless you have different plans for it.

Close a window with an image on Visual Studio 2010

I want to have an image (or a button) that, when the user click on it, it closes the window, like ALT+F4.
Okay...this is an extremely simple operation.
For a Button, handle the Click() event and Close() the Form. You can simply double click the Button to have the method stub inserted for you. The exact same thing can be done with a PictureBox:
VB.Net:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Close()
End Sub
Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
Me.Close()
End Sub
End Class
C#:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
this.Close();
}
}

trying to learn animation in WPF with CompositionTarget.Rendering

I am trying to figure out how to animate/move elements in WPF with VB.NET. It was suggested to me to try using CompositionTarget.Rendering but I am having difficulty getting started. Below is my code and below that is the error message I am getting. Could someone please tell me where I am going going wrong.
Code:
Imports System.ComponentModel
Class MainWindow
Dim flake0 As New flake
Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
canvas1.DataContext = flake0
AddHandler CompositionTarget.Rendering, AddressOf compo
End Sub
Private Sub compo(ByVal sender As Object, ByVal e As EventArgs)
flake0.move()
End Sub
End Class
Error:
Handles clause requires a WithEvents variable defined in the containing type or one of its base types
I had another method that accidentally got "handles CompositionTarget.Rendering" tagged to the end of it.

How to redirect events from a WPF UserControl to WinForm?

I have a winform with a wpf usercontrol on it (ElementHost1). The usercontrol contains only a button. How can I know when the wpf button has been clicked in my winform? How can I "redirect" the events from wpf usercontrol to winform?
Thanks.
This link might be helpful to you.
Or a simple event handling in VB.NET
Public Event ClickMe()
Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
RaiseEvent ClickMe()
End Sub
Then in your actual window you can have this:
Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AddHandler SampleClick1.ClickMe, AddressOf Sample_Click
End Sub
Private Sub Sample_Click()
MessageBox.Show("This is a proof!")
End Sub
That SampleClick1 variable is from the designer code generated available to the form for your use.
Friend WithEvents ElementHost1 As System.Windows.Forms.Integration.ElementHost
Friend SampleClick1 As WindowsApplication1.SampleClick
Here is the one solution i found
in UserControl1.Xaml.cs
public static RoutedEvent ChkBoxChecked = EventManager.RegisterRoutedEvent("CbChecked", RoutingStrategy.Bubble,
typeof(RoutedEventHandler), typeof(CheckBox));
public event RoutedEventHandler CbChecked
{
add
{
AddHandler(ChkBoxChecked, value);
}
remove
{
RemoveHandler(ChkBoxChecked, value);
}
}
private void cbTreeView_Checked(object sender, RoutedEventArgs e)
{
RoutedEventArgs args = new RoutedEventArgs(ChkBoxChecked);
RaiseEvent(args);
}
Now in MainForm Form1 shown event we can add CbChecked event
private void Form1_Shown(object sender, EventArgs e)
{
this.elemetHost1.CbChecked += new System.Windows.RoutedEventHandler(wpfusercontrol_CbChecked);
//elementHost1 is the name of wpf usercontrol hosted in Winform
}
void elementHost1_CbChecked(object sender, System.Windows.RoutedEventArgs e)
{
//This event will raise when user clicks on chekbox
}
i am facing an issue here.i am rasing the same event in Form1 for all checkbox clickevents in UserControl1.so i want to know which checkbox is clicked in the mainform.i tried to see in the RoutedEventArgs e....but doesnt help
how to know which checkbox is clicked in the mainform

WPF navigation via buttons

Question: Is there a way to make a button behave like a hyperlink inside of a user control?
I've been searching around for a few days now and haven't found anyone who has addressed this. How do you use a button to navigate in a WPF application? Specifically how do you make a button inside of a user control navigate it's host frame? Bare in mind that User controls do not have direct access to the host frame. so simply:
this.NavigationService.Navigate(new Uri(this.addressTextBox.Text));
won't work. I'm using user controls. If you are using only pages, this is the answer you are looking for, if you are using UserControls, look at my answer below.
I feel like a goof ball for answering my own question, but i figured it out at long last!
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
Dim pg As Page = CType(GetDependencyObjectFromVisualTree(Me, GetType(Page)), Page)
Dim newPage As %desired uri here% = New %desired uri here%
pg.NavigationService.Navigate(newPage)
End Sub
Private Function GetDependencyObjectFromVisualTree(ByVal startObject As DependencyObject, ByVal type As Type) As DependencyObject
'Walk the visual tree to get the parent(ItemsControl)
'of this control
Dim parent As DependencyObject = startObject
While (Not (parent) Is Nothing)
If type.IsInstanceOfType(parent) Then
Exit While
Else
parent = VisualTreeHelper.GetParent(parent)
End If
End While
Return parent
End Function
This function i found here (http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/f8b02888-7e1f-42f4-83e5-448f2af3d207) will allow for the use of NavigationService inside of a user control.
~N
Use the NavigationService..::.Navigate Method:
void goButton_Click(object sender, RoutedEventArgs e)
{
this.NavigationService.Navigate(new Uri(this.addressTextBox.Text));
}

Resources