How can I select tabitem in usercontrol from MainWindow - wpf

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

Related

Binding UserControl to ViewModel (Caliburn Micro WPF)

I am creating a login form that will be used by many different applications. The login will always have the same logic, so I'd like to bind a viewmodel and do all logic there (Retrieving login info from database, etc). I created a new UserControl, MainView and a ViewModel, MainViewModel both of which are in a Login namespace.
The form continues to run everything in the code-behind, but nothing in the VM.
Is there another way of binding that I am not aware of?
Code-Behind MainView.Xaml.vb
Imports Caliburn.Micro
Namespace Login
Public Class MainView
Public Sub New()
MsgBox("TEST code-behind")
End Sub
End Class
End Namespace
VM MainViewModel.vb
Imports Caliburn.Micro
Namespace Login
Public Class MainViewModel
Inherits PropertyChangedBase
Public Sub New()
MsgBox("TEST ViewModel")
End Sub
End Class
End Namespace
Xaml
<UserControl x:Class="Login.MainView"
xmlns:cal="http://www.caliburnproject.org"
xmlns:local="clr-namespace:cLogin.Login"
cal:Bind.Model="cLogin.Login.MainViewModel" (not sure if needed due to naming)
... >
EDIT
This is how I have added the UserControl as a separate window before the user is logged in, I can see the content, but none of the properties inside the ViewModel bind
Dim login As New Window
With login
.WindowStyle = WindowStyle.None
.ResizeMode = ResizeMode.NoResize
.SizeToContent = SizeToContent.WidthAndHeight
.Content = New MainView()
End With
login.ShowDialog()
Since you are creating the window explicitly, you also need to explicitly set its DataContext:
Dim login As New Window
With login
.WindowStyle = WindowStyle.None
.ResizeMode = ResizeMode.NoResize
.SizeToContent = SizeToContent.WidthAndHeight
.Content = New MainView()
.DataContext = New MainViewModel()
End With
You should also bind the attached Bind.Model property to the DataContext in the view:
cal:Bind.Model="{Binding}"

Call mainwindows from UserControl

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

Access controls on User Control

I have a user control, and I need to access a label on that user control from another Window.. example... simply change the text of a label. Example..
Usercontrol.label1.content = "Got it"
I can access any control on the MainWindow by doing the following:
Public main As MainWindow = DirectCast(Application.Current.MainWindow, MainWindow)
How do I do this for a User Control in WPF?
To access MainWindow:
Dim Main = TryCast(Application.Current.MainWindow, MainWindow);
If your UserControl defined statically like this:
<local:UserControl1 x:Name="uc" />
You can just use it's name like: Main.uc.label1.Text = "Hello World"
If your UserControl is dynamically added to a container, try following:
If your UserControl is in a Border:
Dim control = Main.MyBorder.Child as MyUserControl
control.label1.Text = "Hello World"
If there are multiple UserControls in your container such as (StackPanel/Grid/Wrappanel etc):
Dim controls = Main.MyStackPanel.Childern.OfType(Of MyUserControl)()
For Each control In controls
control.label1.Text = "Hello World"
Next
Even though you can get the exact UserControl you want by checking the variables it has!
Dim control = Main.MyStackPanel.Childern.OfType(Of MyUserControl)().Where(Function(x) x.label1.Text = "myLabel").FirstOrDefault()
//You can access any variable that exists in your UserControl by 'x'
control.label1.Text = "Hello World"
Once you have got a reference to the window in which the UserControl resides, you could access it using this reference.
So if the UserControl is for example defined in a window called Window1, here is how you could access it from another window:
Dim window1 As Window1 = Application.Current.Windows.OfType(Of Window1).FirstOrDefault()
window1.uc.label1.Text = "1"
This of course assumes that there is a Window1 opened and visible on the screen and that you given the UserControl element an x:Name in the XAML markup of the window:
<local:UserControl1 x:Name="uc" />

How to access control of another class in mainwindow WPF using vb

I'm trying to call text box in another class from the MainWindow in WPf using this code:
Private Sub Button_Click_1(sender As Object, e As RoutedEventArgs)
Mainwindowtext2.Text = DirectCast(Application.Current.MainWindow, Window1).text1.Text
End Sub
When I compile there are no errors, but after a Button Click this error comes out
InvalidCastException was unhandled
"Unable to cast object of type 'WpfApplication1.MainWindow' to type 'WpfApplication1.Window1'."
Is there another way to get the control from another class while in MainWindow?
The answer is in the error code you're getting:
Unable to cast object of type 'WpfApplication1.MainWindow' to type 'WpfApplication1.Window1'.
The object you have is of type 'WpfApplication1.MainWindow'. This means that you can't cast it to 'WpfApplication1.Window1'.
Because your root namespace is 'WpfApplication1', the code should read:
Private Sub Button_Click_1(sender As Object, e As RoutedEventArgs)
Mainwindowtext2.Text = DirectCast(Application.Current.MainWindow, MainWindow).text1.Text
End Sub
To understand why, the static property Application.Current is holding a reference to the class Application. It's MainWindow property is of type Window. In your application, your main window is type MainWindow, which derives from Window, and can therefore be returned from the Application.Current.MainWindow property. That is why you need to cast it to MainWindow.
EDIT
If you are trying to get an instance of a different window than the main window, you can use the application's Windows property:
Mainwindowtext2.Text = Application.Current.Windows.OfType(Of Window1)().First().text1.Text
The way WPF creates the backing fields, this should work as long as the classes are in the same assembly. If they aren't, consider creating a read-only property to expose the text, or set the FieldModifier property in XAML.
Please change your code:
Mainwindowtext2.Text = DirectCast(Application.Current.Window1, Window1).text1.Text
Hope This helps you...
try this...
You can use the Binding concept as below:
in .CS File:
Private _TextBoxName As String
Public Property TextBoxName() As String
Get
Return _TextBoxName
End Get
Private Set(ByVal value As String)
_TextBoxName = value
End Set
End Property
in Xaml:
< TextBox Text="{Binding TextBoxName}"/>
Now you can use the value of Textbox as Follows:
Window1textbox.Text = mainwindow.TextBoxName

How can I access one window's control (richtextbox) from another window in wpf?

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;
}
}

Resources