UserControl passes click event to MainWindow Twice - wpf

I have finally gotten my UserControl to do everything I need, generating a button with an icon and text, but when I try to pass the click event up to my Main Window, I find it fires twice! In the abbreviated code below, my test MessageBox comes up twice in response to a single click. That is, it comes up once. I click OK and it comes up a second time. After I click OK the second time it continues. Thank you for looking this over.
<UserControl x:Class=ImageButton"
....
<Button Style="{DynamicResource ImageButtonStyle}" Click="uc_Click"/>
</UserControl>
----
Public Class ImageButton
Public Event Click as RoutedEventHandler
....
Private Sub uc_Click(sender as Object, e As RoutedEventArgs)
RaiseEvent Click(Me, e)
End Sub
End Class
----
<Window x:Class="MainWindow"
....
<local:ImageButton x:Name=NewItem" Click="NI_Click"></local:ImageButton>
----
Class MainWindow
....
Private Sub NI_Click(sender As Object, e As RoutedEventArgs) Handles NewItem.Click
MessageBox.Show("Success")
End Sub
End Class

You add two event handlers in the XAML and in the code-behind.
Just remove Handles NewItem.Click from MainWindow class code behind:
Private Sub NI_Click(sender As Object, e As RoutedEventArgs)
MessageBox.Show("Success")
End Sub
Or remove Click="NI_Click" from view:
...
<local:ImageButton x:Name="NewItem"></local:ImageButton>
...

Related

Mouse double click windowsformhost on a usercontrol with a spreadsheet

I have a usercontrol that contains a spread and I have this methods
Public Event DOBLECLICK()
Public Sub sp1_CellDoubleClick(sender As Object, e As FarPoint.Win.Spread.CellClickEventArgs)
RaiseEvent DOBLECLICK()
End Sub
and in the mainwindow.xaml in the function MainWindow_Loaded I have:
AddHandler host.sp1.CellDoubleClick, AddressOf host.sp1_CellDoubleClick
my question is, how can i use the event of double click and when clicked i hide the windowsformhost i know that i can hide it whit
WinFormsHost.Visibility = Windows.Visibility.Hidden but how to when clicked the doubleclick on the spread.
finally i got it,
in the user control put this:
Public Event Dobleclick()
Private Sub sp1_CellDoubleClick(sender As Object, e As FarPoint.Win.Spread.CellClickEventArgs) Handles spEmpresas.CellDoubleClick
RaiseEvent Dobleclick()
End Sub
and in the MainWindow.xaml:
Imports nameofyourprogram.Control
Public Class MainWindow
Dim host As New nameofyourprogram.Control
Public Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
AddHandler host.Dobleclick, AddressOf doubleclick
end sub
Sub doubleclick()
msgbox("now you can work whit and event in your mainwindow ")
'after this message i want to hide my windownforhost
WinFormsHost.Visibility = Windows.Visibility.Hidden
end sub
End Class

WPF Combobox Item Update

I am rather new to the WPF setup and I am running into an issue where as far as I can see I have set it up correctly to have my combobox bound to a observable collection of object.
The Combobox will update when I add or delete items. If I make a change the items in the drop down will not show any differently but if I select one that was edited it will now show the new information but only when selected.
I have set up the object class to use INotifyPropertyChanged correctly I think but it does not seem to be functioning. Going to attach the code below so that you can easily see exactly what I am trying to describe.
What I am trying to do it allow a user to push a button and have the text inside a combobox update to show the new text.
Imports System.ComponentModel
Public Class Window2
Public _names As New System.Collections.ObjectModel.ObservableCollection(Of TestClass)
Public Sub BaseLoading() Handles MyBase.Loaded
Dim AddNewItem As New TestClass
AddNewItem.groupName = "Item " + (_names.Count + 1).ToString
_names.Add(AddNewItem)
cbo_Names.SetBinding(ItemsControl.ItemsSourceProperty, New Binding With {.Source = _names})
End Sub
Private Sub button_PreviewMouseDown(sender As Object, e As MouseButtonEventArgs)
Dim AddNewItem As New TestClass
AddNewItem.groupName = "Item " + (_names.Count + 1).ToString
_names.Add(AddNewItem)
_names(0).groupName = ("Value Changed")
End Sub
End Class
Public Class TestClasss
Implements INotifyPropertyChanged
Public _groupName As String = ""
Public Property groupName As String
Get
Return _groupName.ToString
End Get
Set(value As String)
_groupName = value
onPropertyChanged(New PropertyChangedEventArgs(_groupName))
End Set
End Property
Public Event PropertyChagned(ByVal sender As Object, ByVal e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged
Public Sub onPropertyChanged(ByVal e As PropertyChangedEventArgs)
RaiseEvent PropertyChagned(Me, e)
End Sub
End Class
XAML
<Window x:Class="Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<Button x:Name="button" Content="Button" PreviewMouseDown="button_PreviewMouseDown"/>
<ComboBox x:Name="cbo_Names" Margin="30,5,30,5" IsEditable="False" ItemsSource="{Binding _names, NotifyOnSourceUpdated=True,Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="groupName" SelectedItem="{Binding _names, NotifyOnSourceUpdated=True,Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
</StackPanel>
</Window>
I would appreciate any help locating what I am missing.
You should pass the name of the data-bound property (instead of the value of the property) to the constructor of the PropertyChangedEventArgs:
onPropertyChanged(New PropertyChangedEventArgs("groupName"))
If you are using at least Visual Studio 2015, you could consider making the following change to your onPropertyChanged routine:
Public Sub onPropertyChanged(<System.Runtime.CompilerServices.CallerMemberName> Optional ByVal propertyName As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
Then, in the setter for groupName you can call onPropertyChanged without specifying the property name, and it will be taken from the name of the caller (that is, it will end up being "groupName").
Effectively, this is doing the same thing as the previous answer, but in a way that is easier for you to code and maintain. (Along with the <CallerMemberName> attribute, this works well with NameOf, both making your code more robust against any changes in names of properties.)

Can't declare the viewmodel in XAML for this code

I've reviewed as many 'how to declare the viewmodel in XAML' posts I can and still can't figure this out. I'm using the simple program below to learn the basics regarding binding and this code works. When I click to insert(add) items, the listbox automatically reflects that change, as well as when I clear the list.
See my question(s) after the code.
Model
Namespace MVVM3
Public Class MyListItem
Public Property MyListItemID() As Integer
Public Property Name() As String
End Class
End Namespace
ViewModel
Namespace MVVM3
Public Class ViewModel
Implements INotifyPropertyChanged
Public Property allIdeas() As New ObservableCollection(Of MyListItem)
Public Sub New()
For index = 0 To 9
Dim anItem As New MyListItem
anItem.Name = "Idea " & index
allIdeas.Add(anItem)
Next
End Sub
Public Sub InsertAnItem()
Dim anItem As New MyListItem
anItem.Name = "Item " & allIdeas.Count()
allIdeas.Add(anItem)
NotifyPropertyChanged("allIdeas")
End Sub
Public Sub ClearStoredList()
allIdeas.Clear()
NotifyPropertyChanged("allIdeas")
End Sub
Public Event PropertyChanged As PropertyChangedEventHandler _
Implements INotifyPropertyChanged.PropertyChanged
Public Sub NotifyPropertyChanged(ByVal propertyName As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
End Class
End Namespace
View
<Window x:Class="MVVM3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:mvvm3"
Title="MainWindow" SizeToContent="WidthAndHeight" WindowStartupLocation="CenterScreen"
>
<StackPanel Grid.Column="1">
<Button Margin="25" Content="Insert an item" Click="InsertAnItem_Click"/>
<Button Margin="25" Content="Clear stored list" Click="ClearStoredList_Click"/>
<ListBox Name="listBox3" ItemsSource="{Binding allIdeas}" DisplayMemberPath="Name" Height="100">
</ListBox>
</StackPanel>
</Window>
Code Behind
Namespace MVVM3
Partial Class MainWindow
Private vm = New ViewModel
Sub New()
InitializeComponent()
DataContext = vm
End Sub
Private Sub InsertAnItem_Click(sender As Object, e As RoutedEventArgs)
vm.InsertAnItem()
End Sub
Private Sub ClearStoredList_Click(sender As Object, e As RoutedEventArgs)
vm.ClearStoredList()
End Sub
End Class
End Namespace
I want to move the ViewModel declaration into XAML to eliminate code behind. If I comment out DataContext = vm, no matter what method I've followed from various posts, the binding no longer updates the listbox.
The following changes result in the listbox showing the initial assignment that takes place in ViewModel.New, but after that no changes are reflected:
<Window x:Class="MVVM3.MainWindow"
...
xmlns:local="clr-namespace:mvvm3"
xmlns:vm="clr-namespace:mvvm3.MVVM3"
...
>
<Window.DataContext>
<vm:ViewModel/>
</Window.DataContext>
What am I missing? Is it a namespace problem?
I'm hoping to move on to Commands and ViewModel locators, but I don't see how I can do that until I understand this.
When you declare an instance of the ViewModel class in XAML, you may access it in code behind by casting the value of the DataContext property to the ViewModel type:
Partial Class MainWindow
Sub New()
InitializeComponent()
End Sub
Private Sub InsertAnItem_Click(sender As Object, e As RoutedEventArgs)
CType(DataContext, ViewModel).InsertAnItem()
End Sub
Private Sub ClearStoredList_Click(sender As Object, e As RoutedEventArgs)
CType(DataContext, ViewModel).ClearStoredList()
End Sub
End Class

Event not fired .NET / XAML

I have a button and when it is clicked, an event should be raised. I declare the event in one class, but I invoke it in another.
Here is my code:
XAML:
<Button x:Name="btnAddUsers" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="0,0,15,60" Padding="5,0,5,0" Click="ImportButton_Click" Content="Add"></Button>
Event declaration and the Click method:
Public Event GettingADUsers()
Private Sub ImportButton_Click(sender As Object, e As RoutedEventArgs) Handles btnADAddUsers.Click
RaiseEvent GettingADUsers()
End Sub
In another class I invoke the event (I invoke it in the constructor):
AddHandler StaffSettings.GettingADUsers, AddressOf ctlStaff_GettingAllUsers
And the method:
Private Sub ctlStaff_GettingAllUsers()
adUsers.Load()
End Sub
Do I miss something?

Accessing the WPF element in parent window from the user control

I have a custom menu control in the main window of my WPF application. In main window, I have a frame. I want to change the page in this frame based on the selection in the custom menu control. Below is the code I tried.
Private Sub NominationMenuItem_Click(sender As Object, e As RoutedEventArgs)
Dim parentWindow As Window
parentWindow = Application.Current.MainWindow
parentWindow.MainFrame.Navigate(New NominationSearch)
End Sub
I know that I can't directly access "MainFrame" control using the "parentWindow" object. How do I rewrite the last line to get a reference to the existing frame object.
It's not appropriate for UserControl to manipulate it's parent. You should let your MainWindow to listen for NominationMenuItem changes via events and then MainWindow itself do the navigation.
So first you need to define an event in your UserControl:
Public Event NominationMenuItemChanged As RoutedEventHandler
Private Sub NominationMenuItem_Click(sender As Object, e As RoutedEventArgs)
RaiseEvent NominationMenuItemChanged(Me, New RoutedEventArgs())
End Sub
And then listen to this event in your MainWindow:
Public Sub MainWindow()
AddHandler MyUserControl.NominationMenuItemChanged, AddressOf UC_NominationMenuItemChanged
End Sub
Private Sub UC_NominationMenuItemChanged(sender As Object, e As RoutedEventArgs)
MainFrame.Navigate(MyUserControl.NominationSearch)
End Sub

Resources