How to Bind to Property Which Resides in the App Xaml backend - wpf

I have a Property in my App.xaml.cs called User that holds the User details.
I have read here that you can't have a dependency property on the App class.
I chose to use App.cs because it is global to the entire program and this is used for access control, but any alternatives are welcome.
Now my question is how can I bind to this property from my UserControls and Windows.
IsEnabled="{Binding Path=User, Converter={StaticResource hasAccessConverter}, ConverterParameter=Mid}"
This obviously only works on a property on the DataContext. I want to access the property on the App.
Can someone show me an example of the binding to the App.xaml.cs property if I implement INotifyChanged?

I have read here that you can't have a dependency property on the App class.
Indeed you can't, because Application doesn't inherit from DependencyObject. However, it's not necessary : only the target property of a binding needs to be a dependency property.
If you want to bind to a property of your App class, you can do it like that :
IsEnabled="{Binding Path=User, Source={x:Static Application.Current}}"

And to access it programatically in C# you can do this:
((App)Application.Current).YourMethod
((App)Application.Current).YourProperty

You can bind declaratively in Silverlight to Application.Current by using a custom converter.
See my blog post here

Related

How can I bind Text property of TextBox (VIEW) to a vaiable (in VIEWMODEL)

I am a newbie in WPF. I was exploring MVVM Pattern for WPF applications. I am having trouble in binding Text property of a TextBox from VIEW to a variable in VIEWMODEL
Here is the TextBox from MainWindow.xaml
<TextBox x:Name="UsernameTxt" Grid.Row="4" materialDesign:HintAssist.Hint="Username"/>
I just need to know how to bind its Text Property to ViewModel Class in Class Library
Thanks
I think it's possible to give a very generic answer to this very generic question.
If the question changes context this answer is very likely to be deleted but here goes anyhow.
You want your viewmodel to be in the datacontext of the textbox. Because datacontext is inherited down the visual tree this usually means you want to set datacontext of your window to an instance of the viewmodel. Or maybe the usercontrol your textbox is in, but we know nothing about your app so let's just cover the simple scenario.
Your options are to instantiate a viewmodel using code or xaml.
If you look at this article:
https://social.technet.microsoft.com/wiki/contents/articles/31915.wpf-mvvm-step-by-step-1.aspx
That instantiates in xaml.
Note the xmlns is
xmlns:local="clr-namespace:wpf_MVVM_Step01"
That's saying where you see some bit of markup which is prefaced "local:" then go get the class out of this namespace.
To point to a different dll ( a class library ) you need to tell it which assembly. You do that by adding ;assembly=Whicheverdll to your equivalent of that xmlns. And of course that won't be local then so give it a different name. You also need a reference to that dll or project added to the entry point exe.
Once you've done all that and your viewmodel is instantiated into memory and in the datacontext of that textbox you need some sort of binding.
Which the article covers but that will be something like:
<TextBox Text="{Binding YourPublicStringProperty}"/>

WPF MVVM - How to Bind Custom Control->ToggleButton.IsChecked to View->TextBox.Text

I am moving over from WinForms to WPF and trying to implement the MVVM pattern for a touchscreen application. I created several custom controls inside a WPF Control Library (dll), and I can bring these controls into the View with no issue. However, I am getting stuck on a purely academic scenario where I want a TextBox inside the View to display my custom control's ToggleButton.IsChecked property as "Checked" and "Unchecked" respectively.
To sum up, I need to know the proper way to expose properties of a control that is inside a custom user control. Then when the exposed property changes update some other control with custom data based on the property that changed.
To sum up, I need to know the proper way to expose properties of a control that is inside a custom user control. Then when the exposed property changes update some other control with custom data based on the property that changed.
You're describing dependency properties. You need to add a dependency property to your custom control, which you then bind to from inside the control, and from outside it (in your view).
The first part will depend on whether you're using a UserControl or a Control. Let's say it is a Control, then you would use a TemplatedParent binding in your ControlTemplate:
<ToggleButton IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent},Path=IsToggleChecked,Mode=TwoWay}" ... />
If on the other hand it is a UserControl, then the approach is similar, but you need to make sure the data context is right. One approach would be to use a FindAncestor binding:
<ToggleButton IsChecked="{Binding RelativeSource={RelativeSource AncestorType=UserControl},Path=IsToggleChecked,Mode=TwoWay}" ... />
Now, to add the dependency property, try the Visual Studio code snippet "propdp". It should look something like this:
public bool IsToggleChecked
{
get { return (bool)GetValue(IsToggleCheckedProperty); }
set { SetValue(IsToggleCheckedProperty, value); }
}
public static readonly DependencyProperty IsToggleCheckedProperty =
DependencyProperty.Register("IsToggleChecked", typeof(bool), typeof(MyCustomControl), new PropertyMetadata(false));
And now finally you can bind your TextBox to the new dependency property:
<TextBox Text="{Binding ElementName=myCustomControl,Path=IsToggleChecked,Converter={StaticResource BoolToTextConverter}}" />
<local:MyCustomControl x:Name="myCustomControl" ... />
I assumed that you would want to make an IValueConverter "BoolToTextConverter" that converts the boolean value to the string "Checked" or "Unchecked".

Setting binding of subproperty

I have an element on the my usercontrol
<myControls:MonitorWindow x:Name="WindowFrame" MinHeight="400" />
and I need to change its subproperty
MonitorWindow.CloseButton.Visibility.
Is it possible bind property from ViewModel to that property?
One way is ElementBinding with TwoWay Mode to fake field, but it isn't very nice.
You should Make a Dependency Property of the Type Visibility so that its can be binded to your control and can be set from outside the control.
For Ex Let the Dependency Property is named as CloseButtonVisibility is present in codebehaind of your user Control (use propdp then press TAB or double TAB as snippet shortcut to create one)
IN Your Control
<UserControl Name="Control">
<Button Visibility="{Binding Path=CloseButtonVisibility,ElementName=Control,Mode=TwoWay}"></Button>
</UserControl>
While using your Control
<myControls:MonitorWindow x:Name="WindowFrame" MinHeight="400" CloseButtonVisibility="Hidden" />
This might help... :)
You should do two things:
First you should expose the CloseButtonVisibility as a property.
The second thing is, in order to bind value to this property you should define this property as Dependcy property, see the following link for explanation on how to define dependency property:
UNDERSTANDING: DEPENDENCY PROPERTIES IN SILVERLIGHT
Good Luck,
Lior

Silverlight Control property as a data binding Source and View-Model property as target

I have a property on a Silverlight control that a ViewModel wants to bind to. The ViewModel need to told of changes to the property NOT the other way around
Syntax like
<MyControl ViewPort="{Binding VMProperty}"/>
Declares ViewPort as the Target, in this instance ViewPort is the source of the data. I know I could make it TwoWay binding but that just seems wrong when i simply want one way but in the other direction.
Besides I do not want to make the property on the control a DependencyProperty because I do not want that property settable and I do not beleive that Silverlight supports read only dependency properties.
Is there a different way of setting up the Binding?
TIA
Pat Long
Maybe this works?
http://forums.silverlight.net/forums/p/141042/315359.aspx#315359
{Binding ElementName=TextBox1, Path=Text, Mode=TwoWay ,UpdateSourceTrigger=Explicit}

Implementing a Property Inspector/Editor in WPF

So far my plan is to have an event "Item selected" which the property inspector listens to. The actual property inspector is just a ContentControl. When the object is selected the content property is set and the appropriate DataTemplate for editing the object is loaded.
In general I am trying to do this "MVVM" style. I guess you could use reflection instead of templating but I only have a handful of types so far.
Has anyone implemented something similar?
Can you offer any advice or source code?
Basically, what you're looking for is a PropertyGrid... have a look at this : http://www.codeplex.com/wpg
If you want to do this MVVM style, then instead of having a ItemSelected event and using code to set the inspector's content, have a SelectedItem property in your viewmodel, and bind the inspector's Content to that property:
<ContentControl Content="{Binding SelectedItem}" />
How you update the SelectedItem will depend on the nature of your view and model. For example, if the items are displayed in a Selector control like a ListBox, then you would just two-way bind the Selector.SelectedItem to the viewmodel's SelectedItem.
Have a look at the WPF Inspector project. It's a spy utility like Snoop but it also includes a feature to debug triggers.

Resources