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

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

Related

WPF Dependency Property - set from XAML

I am trying to achieve the following in a WPF personal finance app:
In various places I want to display a user control giving details of a asset holding (usually a share, bond etc), the target asset may be changed dynamically by the user in which case the control must be refreshed. Each Asset has a unique identifier, AssetId.
I am using MVVM and I've developed a single window with a View Model that takes AssetID as a parameter (property) and retrieves the relevant details for binding to the View. This work fine. What I'd like to do is make a generic user control with the same functionality so I can basically drop that 'window' inside other windows.
So I pretty much copy-pasted the XAML from that form into a User Control, where I'm struggling is passing in the AssetId from the parent window to the child control.
Google tells me I need a dependency property and here's where I am
Public Class HoldingView
Private _AssetId As Integer
Public AssetIdProperty As DependencyProperty = DependencyProperty.Register("AssetId",
GetType(Integer),
GetType(HoldingView),
New FrameworkPropertyMetadata(New PropertyChangedCallback(AddressOf AssetIDChanged)))
Public Property AssetId As Integer
Get
Return GetValue(AssetIdProperty)
End Get
Set(value As Integer)
SetValue(AssetIdProperty, value)
End Set
End Property
Private Sub AssetIDChanged(d As DependencyObject, e As DependencyPropertyChangedEventArgs)
Dim NewAssetId As Integer
NewAssetId = e.NewValue
Me.DataContext.AssetId = NewAssetId
End Sub
Public Sub New()
' This call is required by the designer.
InitializeComponent()
Me.DataContext = New HoldingViewmodel
End Sub
End Class
Called like this:
<Grid>
<local:HoldingView AssetId="{Binding AssetId}"/>
</Grid>
The code compiles and runs but when I try and load the window that has the user control, the app crashes with this message:
A 'Binding' cannot be set on the 'AssetId' property of type 'HoldingView'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.
Which is not that helpful. From my Googling, you can also get this message if the syntax of the DP registration is not spot on, but it looks Ok to my inexperienced eye...
Anybody else had this?
Public AssetIdProperty As DependencyProperty
should be
Public Shared ReadOnly AssetIdProperty As DependencyProperty
Please take a look at Custom Dependency Properties.
Also remove
Me.DataContext = New HoldingViewmodel
because that will effectively break any DataContext-based Bindings like
AssetId="{Binding AssetId}"
where the source property is supposed to be owned by the object in the inherited DataContext, which usually is an object in the application's view model.
Controls should never have their own, "private" view model, but instead handle property changes in code behind. In case of UserControls, there could simply be UI elements in their XAML that would be bound to the UserConrol's own properties.
Hence
Me.DataContext.AssetId = NewAssetId
in the PropertyChangedCallback is pointless and should be removed, as well as
Private _AssetId As Integer
To summarize, it should look like this:
Public Class HoldingView
Public Shared ReadOnly AssetIdProperty As DependencyProperty =
DependencyProperty.Register(
"AssetId",
GetType(Integer),
GetType(HoldingView),
New FrameworkPropertyMetadata(
New PropertyChangedCallback(AddressOf AssetIdPropertyChanged)))
Public Property AssetId As Integer
Get
Return GetValue(AssetIdProperty)
End Get
Set(value As Integer)
SetValue(AssetIdProperty, value)
End Set
End Property
Private Shared Sub AssetIdPropertyChanged(
d As DependencyObject, e As DependencyPropertyChangedEventArgs)
CType(d, HoldingView).AssetIdChanged(e.NewValue)
End Sub
Private Sub AssetIdChanged(id As Integer)
...
End Sub
Public Sub New()
InitializeComponent()
End Sub
End Class

Showing saved Xaml Canvas through binding

I'm saving an svg image as xaml string in a database. Next I record this xaml string in a property of a class (MyClass.XamlString) .
On my form I have a Canvas, and I want MyClass.XamlString to be the child of the canvas.
This is what I have:
Dim MyBinding As New Binding("XamlString")
MyBinding.Source = MyClass
MyBinding.Converter = New clsXamlToCanvasConverter
CanvasOnForm.SetBinding(ContentPresenter.ContentProperty, MyBinding)
and
Friend Class clsXamlToCanvasConverter
Implements IValueConverter
Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.Convert
Dim ConvertedCanvas As Canvas = System.Windows.Markup.XamlReader.Parse("<Canvas xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">" & value & "</Canvas>")
Return ConvertedCanvas
End Function
Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
Throw New NotImplementedException
End Function
End Class
In debugging I see the clsXamlToCanvasConverter creates a canvas with children from the xaml string, this is working correctly although you might say the extra canvas is redundant.
My guess is this is where I'm going wrong: CanvasOnForm.SetBinding(ContentPresenter.ContentProperty, MyBinding)
The code runs fine, the problem is I don't see the image displayed.
I've been searching a lot on this and I've seen this post WPF What is the correct way of using SVG files as icons in WPF , but apparently this hasn't helped me out.
Any ideas?

Hide a property from an observablecollection

So I have a WPF DataGrid bound to an ObservableCollection, which contains a single instance of a class - for example:
Public Class parent
Public Property title As String [...]
Public Property someCommonThing as Integer [...]
Public Class Child Inherits Parent
Public Property name As String [...]
Public Property address As String [...]
Public Class Window1
Dim oc As ObservableCollection(Of Object) = New ObservableCollection(Of Object)
oc.Add(New Child())
dataGrid.ItemsSource = oc
there are many child classes with different properties, hence why I can't easily define the datagrid columns directly.
I want to be able to hide certain parent properties from the datagrid (for example, never show the title property in the datagrid), while still being able to use it for databinding elsewhere (e.g. a label).
Is this possible? I can't think how to do it without manually specifying every column for every possible class instead of using the databinding.
When automatically generating columns you can change the per-property behavior using Data Annotations, in this case specifically the BrowsableAttribute class:
<Browsable(False)>
Annotating your property with this will prevent a column from being generated when using the following event handler on the AutoGeneratingColumn event of the DataGrid.
Private Sub OnAutoGeneratingColumn(sender As Object, e As DataGridAutoGeneratingColumnEventArgs)
If Not DirectCast(e.PropertyDescriptor, PropertyDescriptor).IsBrowsable Then
e.Cancel = True
End If
End Sub
Remember to add the DataAnnotations assembly to your project.

Binding from multiple sources

My scenario:
I've got a Silverlight Application with a View, where i want to bind the textboxes to an object (two-way) and all labels to a dictionary holding the label translations.
My approach was to set the datacontext of the page to a dictionary with two items, one of them is the object and the other is the translation-dictionary.
In xaml the code looks like the following:
<TextBlock Text="{Binding [dict].[name],FallbackValue='Fallback'}" />
<TextBox Text="{Binding [obj].name,Mode=TwoWay}" />
This works initially, if I however change the object on the datacontext, the xaml is not notified about any changes and doesn't update.
I've had a working solution using a Converter for the translations, however due to the limitations on one converterparameter I didn't like the solution. In addition it wasn't possible to place a fallback-value in the textblock, which resulted in "invisible" textblocks while designing the page.
Any suggestions on how to solve this issue? It doesn't have to be using my dictionary, it would be also okay if i could set the datacontext to the object (which works) and bind the labels somehow different.
I know this will get a lot of traditional answers, but I would also like to put forward something completely original we tried (and succeeded) doing ourselves for more efficient localisation of Silverlight using Attached Properties instead of binding:
Localisation of Silverlight projects after completion
What would be the most flexible is rather than setting the DataContext for the view to a dictionary, you would be better off having the DataContext be something like a ViewModel. That is, a simple class that holds multiple properties: one for your "object" and one for your translation dictionary.
Then have the class that acts as your ViewModel implement INotifyPropertyChanged.
Create a method in your class called OnPropertyChanged that takes in a string representing your property name. In that method raise the PropertyChanged event passing in the instance of the ViewModel class and a new PropertyChangedEventArgs passing in the property name.
Back in the properties you created (object and dictionary) in the Set, after setting the value, call OnPropertyChanged passing in the string name of that property. This will notify the UI that the value of this property has changed and will essentially rebind the control to that property.
Finally, bind the Text properties of your controls on your View to the new properties you just created in your ViewModel. That should ensure that the controls on the view stay up to date.
I found a solution, but wasn't able to answer my own question (8h limit..)
I think this is just the approach Hydroslide suggested.
Create a class which holds all data and implements INotifyPropertyChanged
Public Class MyDatacontext
Implements ComponentModel.INotifyPropertyChanged
'Properties
Private _obj As Object
Private _dict As Dictionary(Of String, String)
'Events
Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
'Methods
Public Property Obj As Object
Get
Return _obj
End Get
Set(ByVal value As Object)
_obj = value
'Notify the xaml about the changed Object
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("Obj"))
End Set
End Property
Public Property Dict As Dictionary(Of String, String)
Get
Return _dict
End Get
Set(ByVal value As Dictionary(Of String, String))
_dict = value
'Notify the xaml about the changed translation
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("Dict"))
End Set
End Property
End Class
Define a private var in your page code
Private mycontext As New MyDatacontext
In the constructor of your page, fill your "mycontext" with the desired data
mycontext.Dict = LoadDictionary()
mycontext.Obj = LoadObject()
Me.DataContext = mycontext
Change your xaml to the following
<TextBlock Text="{Binding Dict.[name],FallbackValue=MyFallback}" />
<TextBox Text="{Binding Obj.name,Mode=TwoWay}" />
Update your object/dictionary as you like using
mycontext.Obj = LoadNextObject()

Basic WPF question: How to add a custom property trigger?

I am using Expresion Blend 3 and created a new user control in my project. I want a storyboard to run if a custom property of that user control is triggered like with the ones shown here in the list..
I learnt you need a dependency property, but my understanding there is limited. Here's the basic code I set up with property "IsAwesome" as an example..
Partial Public Class simpleControl
Public Sub New()
MyBase.New()
Me.InitializeComponent()
End Sub
Public Shared ReadOnly IsAwesomeProperty As DependencyProperty = _
DependencyProperty.Register("IsAwesome", GetType(Boolean), GetType(simpleControl))
Public Property IsAwesome() As Boolean
Get
Return DirectCast(Me.GetValue(IsAwesomeProperty), Boolean)
End Get
Set(ByVal value As Boolean)
Me.SetValue(IsAwesomeProperty, value)
End Set
End Property
End Class
However, my property doesn't show in that list. What am I missing? Or is my entire approach wrong?
Any help or advice would be appreciated!
Cheers
I created a new Wpf project. Added a new UserControl (UserControl1) with a custom dependency property called Foo.
Then I opened Blend and added an instance of UserControl1 to Window1. I right clicked on UserControl1 and said EditTemplate | Edit a Copy.
This created a copy of my user control template in the Window.Resources. From within this new template I went up to the Triggers panel and clicked the button to add a new property trigger.
Right away Blend defaulted to selecting my property in the "Activated When" section.
alt text http://blog.BradCunningham.net/Images/ForumImages/CustomDPInBlend.png
You can grab my little sample app from here: http://blog.BradCunningham.net/SourceCode/ForumSamples/CustomDPInBlend.zip

Resources