I have a Window(mainwindows.xaml) where there's a label.Now,i've created a UserControl(Just a basic button)..On Window_load,i'm adding the UserControl in a canvas(in mainwindow) using this :
Dim con as new myUserControl
Canvas.Children.Add(con)
Now What i want is,when i click the UserControl,the label will be hidden..How do i do this ??
I've tried creating a WPF CUSTOM CONTROL LIBRARY but don't know how to work with it ??
Fixed it myself....Guess that was really easy.In usercontrol,just used this :
Dim hm As MainWIndow = Window.GetWindow(Me)
hm.Lbl1.Visibility=Visibility.Hidden
Related
I am new to WPF
I have many master forms.which is having some buttons in all forms.so i wrote that button handing in usercontrols and using this same in all other master forms
now i want to handle each button.for that i wrote code on each form and trying to access it to user control class. but i couldn't. How can i do it ?
I have 3 forms
1)Aform
2)BForm
3)CForm
each form having 2 button.Say Save and Delete
Each form have to differently handle these buttons. So i wrote code on each form as function or Sub
Now I want to call that function to user control..'
I tried like this
in AForm.Vb :
Public Sub SaveA()
{}
In UserControl :
Dim ParentControl as Window=Window.GetWindow(me)
ParentControl.SaveA () // But i couldn't
What can i do for this ?
You need to type-cast the Window object to your specific Window implementation. For example given you defined the function in MainWindow class :
Dim ParentControl as MainWindow = CType(Window.GetWindow(me), MainWindow)
ParentControl.SaveA()
I would like to ask you, how can I access my usercontrol from MainWindow?
For accessing to MainWindow I'm using :
Dim mw As MainWindow = DirectCast(Application.Current.MainWindow, MainWindow)
I need this because I would like to select particular TabItem from my UserControl. (tabitem.IsSelected= true). UserControl is already placed in Grid on MainWindow as a child.
Thank you in advance
simple way is to create a ReadOnly property in MainWindow returning the user control instance
eg
Public ReadOnly Property MyEmployeeMenu As UserControl
Get
Return Me.employeemenu
End Get
End Property
usage
Dim mw As MainWindow = DirectCast(Application.Current.MainWindow, MainWindow)
Dim empMenu as UserControl = mw.MyEmployeeMenu 'this way you can access the control
I've been trying to edit variables in other windows like in VB.NET using DirectCast. This seems to be working very well with the main window, as I use
Private Main As MainWindow = DirectCast(Application.Current.MainWindow, MainWindow)
But, I am unable to find a way to use this with a window other than the main one. For now, I am stuck using this
Dim WindowOne As New Window1
WindowOne.Show()
This works, but I would rather not have to create a new instance of the window each time I want it to open. I have tried using
Private WindowOne As Window1 = DirectCast(Application.Current.Windows.OfType(Of Window1).First(), Window1)
but it always gives me an error saying that "The sequence contains no elements".
Is there any other way to do this? What am I doing wrong?
The correct syntax is below.
Private WindowOne As Window1 = Application.Current.Windows.OfType(Of Window1)().FirstOrDefault()
If Not WindowOne Is Nothing Then
'object is available here
End If
Consider a class that inherits from System.Windows.Window like so:
Public Class MyWindow
Inherits Window
Private _root As Grid
Public Sub New()
MyBase.New()
_root = New Grid
Me.AddChild(_root)
End Sub
Private Sub Me_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
Dim image As New Image
Dim bitmapImage As New BitmapImage(New Uri("Assets/MyImage.png", UriKind.Relative))
image.Source = bitmapImage
image.Width = 100
image.Height = 100
image.Stretch = Stretch.Fill
_root.Children.Add(image)
End Sub
End Class
Let it be part of a WPF Windows Application that has the following module as its startup object:
Module MyModule
Sub main()
Dim myApplication As New Application
myApplication.Run(New MyWindow)
End Sub
End Module
The window gets displayed, but the image does not. When inserting the exact same image loading code in the Loaded event of a VS default MainWindow class (MainWindow.xaml.vb), the image shows up as expected. MyImage.png has 'Build Action' set to 'Resource' in both cases. What am I missing here?
Edit:
I learned that such references in codebehind must be specified using the Pack URI scheme, so replacing the Uri code with
New Uri("pack://application:,,,/Assets/MyImage.png")
will make it work. The problem was that the relative Uri was interpreted as 'file system absolute' (despite having specified UriKind.Relative), and the image location got resolved to C:\Assets\MyImage.png.
But that doesn't answer the underlying question: Why does
New Uri("Assets/MyImage.png", UriKind.Relative)
work when used in the codebehind of the standard MainWindow class (which also inherits Window, but additionally has some associated XAML), but not in a 'bareboned' descendant of Window like the MyWindow class above (defined in code only)?
Apparently, the Uri was interpreted as absolute - even though I specified it as UriKind.Relative.
When replacing with
Dim bitmapImage As New BitmapImage(New Uri("pack://application:,,,/Assets/MyImage.png"))
it works.
I'm sure this is something very simple but I can't figure it out. I've searched here and on msdn and have been unable to find the answer. I need to be able to set the richtextboxes selection via richtextbox.Selection.Select(TextPointer1, Textpointer2).
Application.Current contains a collection of all windows in you application, you can get the other window with a query such as
var window2 = Application.Current.Windows
.Cast<Window>()
.FirstOrDefault(window => window is Window2) as Window2;
and then you can reference the control from your code, as in
var richText = window2.MyRichTextBox
Application.Current.Windows.OfType(Of MainWindow).First
You should be able to access controls on Window1 from Window2 code behind, if that's what you want. Generated fields are internal by default.
All you need is to name the control on Window1, like this:
<RichTextBox x:Name="richtextbox" ... />
In Window2 code behind:
var window = new Window1(); // or use the existing instance of Window1
window.richtextbox.Selection.Select(TextPointer1, Textpointer2);
A better option would be to encapsulate select operation in a method in code behind of Window1, to avoid giving away internal. Then you would have:
// Window1.cs
public void Select(int param1, int param2)
{
richtextbox.Selection.Select(param1, param2);
}
// Window2.cs
var window = new Window1(); // or use the existing instance of Window1
window.Select(TextPointer1, Textpointer2);
You cant access the texbox from another window as it is private to that window you can however work around this by exposing the RichTextBox as a public property on your window (hack)
public RichTextBox RichTextBox {
get{
//the RichTextBox would have a property x:Name="richTextbox" in the xaml
return richTextBox;
}
}