WPF - Bind to MainViewModel property from modal - wpf

It is easy to bind to a property in MainViewModel (MVM) from a button to manipulate whether it is enabled or not:
<Button IsEnabled="{Binding Path=IsUSBInserted}"/>
However, I have a modal pop up (new Window). I would like a button in the pop up window to be controlled by the IsUSBInserted property in MVM as well. Is this possible?
Here's what I am trying to do conceptually (in the pop up XAML): 1) is it possible 2) if so, what is correct syntax as this does not seem to be working. Thank you.
<Window xmlns:mainViewModel="clr-namespace:Acme.MainApp.ViewModel">
<Button IsEnabled="{Binding Path=mainViewModel.IsUSBInserted}"/>

I would like a button in the pop up window to be controlled by the IsUSBInserted property in MVM
There are two ways to do this,
Provide the viewmodel into the popup's datacontext then bind to the property in question.
Create a dependency property on the popup window where one would bind to the VM's property to allow such linkage.

Related

Child window in MVVM light

How can I use Messaging from MVVM Light in am WPF app for showing a ChildWindow and returning some values from it ?
what I need is to present to user a modal dialog with 2 datepickers and the message to return somehow theese 2 values for using as parameters to other views.
Is it possible ?
Usually I just use a Popup for something like this.
The Popup usually has its Visibility bound to something like an IsPopupVisible property in the ViewModel, and the Popup DataContext is usually also part of the ViewModel
I'm not a huge fan of WPF's default Popup control, so I have my own custom Popup control here if you're interested.
It's typically used like this:
<local:PopupPanel Content="{Binding PopupContent}"
local:PopupPanel.PopupParent="{Binding ElementName=ParentPanel}"
local:PopupPanel.IsPopupVisible="{Binding IsPopupVisible}" />
and I show it from the ViewModel like this:
PopupContent = new SelectDatesViewModel();
IsPopupVisible = true;
Once the SelectDatesViewModel.SaveCommand gets triggered, you can fire off a message containing the selected dates to whatever ViewModels are interested:
Messenger.Default.Send(new DatesChangedMessage(this.Date1, this.Date2))
The actual popup content can either be defined in the XAML, or with an implicit DataTemplate
<DataTemplate DataType="{x:Type local:SelectDatesViewModel}">
<local:SelectDatesView />
</DataTemplate>

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

How to clear a TextBox in a User Control in WPF C#?

I have a user control that has a textbox in it, and i am using a clear button on my main form to clear information from the entire main window. i would like to clear the textbox in the user control once the clear button is clicked as well. i havent found an easy way to do this. i have tried referencing the control's name in c# followed by a "." however the name of the text box does not show up. any help would be appreciated!
WPF declares controls in a UserControl as private. To make your TextBox public you declare it with a FieldModifier as in:
<TextBox x:FieldModifier="Public" />
where x is the xaml namespace xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml". However the recommended way of clearing a TextBox is to bind it to a property and then clear the property.
You should not try to directly access controls within a UserControl from external classes or code. The simple mechanism would be to add a Clear() method to the UserControl which clears all relevant controls and information inside the UserControl.
The textbox could be bound to the DataContext of the UserControl. So a way of clearing it might be setting the property that is bound to the Text property of the TextBox to an empty string.

Setting a property on the ViewModel from the View in WPF

I have a dependency property on my ViewModel which is the DataContext for my View. The ViewModel has no reference to the View. The property on the ViewModel is going to reference a control on the view, but I need to be able to set this property in XAML.
How is this possible? One thought I had was to develop a custom control which has a Property property and a Value property, so you could do something like this in the View to set the property on the ViewModel:
<PropertySetter Property="{Binding MyViewModelDependencyProperty}" Value="{Binding ElementName=aControlOnMyView" />
Before I went down this route, I wanted to check if there was any other approach I could take?
Thanks for the detailed reply Ray, but if I give you a bit more detail about the problem I'm trying to solve, you might get a better idea of why I mentioned the approach I did.
Basically, what I'm trying to do is set the focus to a textbox when the user hits a button. I've written an attached property which you can attach to the Button control, specify what the trigger event is (in this case the 'Click' event), and then what control to focus on. This works really nicely, and keeps everything in XAML.
However, I now have a use case where the focus should be set to an arbitrary text box from the click event on a button which is part of a toolbar. This toolbar is itself a user control which is sitting inside another user control, which is inside another user control! This toolbar needs to be reusable across various different forms, and each time, the control to set focus on after you click the button will be different per form.
That's why I had the idea of making the focus control (i.e. a textbox) a property on the view model itself (on my ViewModel base to be precise), and have the ViewModel base code (which the toolbar is bound to), set the focus to the control when the button is clicked (and the e.g. Add/Edit method is called on the ViewModel base).
In unit test land, the control to focus on property will be null, so it's .Focus() method just won't be called. So I can't see an issue there. My problem is then how you set the focus control property from XAML, which is why I had the PropertySetter idea.
I don't like the fact that the ViewModel has any reference to controls sitting on the view, but I can't see another way to achieve what I need. What if the logic that dictates whether to set focus to the control is quite complex? This would sit in the ViewModel surely? Therefore, is there any harm in the ViewModel having this UIElement property? It still knows nothing about the specific View it is bound to, it just knows that there is a control which it needs to set focus to when some action happens on the ViewModel.
My first reaction (and it's a strong one) is so say "Don't do that!" By giving your view model a reference to a part of your UI you are breaking the encapsulation that makes view models so powerful and useful.
For example, what if you want to unit test your view model or serialize it to disk? In each case the piece of your UI will not be present, because there will be no view at all. Your tests will miss coverage and your reconstitution will be incomplete.
If your view model actually needs references to UI objects and there is no better way to architect it, the best solution is to have the view model itself construct those controls it requires a reference to. Then your view can incorporate that control as the Content of a ContentPresenter via binding and provide a Style to configure the control, including a ControlTemplate to provide its content. Thusly:
public class MyViewModel
{
public ListBox SpecialControl { get; set; }
public MyViewModel()
{
SpecialControl = new ListBox();
}
}
and
<DataTemplate TargetType="{x:Type local:MyViewModel}">
<DataTemplate.Resources>
<Style TargetType="ListBox" ... />
</DataTemplate.Resources>
...
<ContentPresenter Content="{Binding SpecialControl}" />
</DataTemplate>
Other possibilities are:
Have the view model actually derive from the Control class, then override OnApplyTemplate() and use GetTemplateChild to find a template item whose name starts with "PART_"
Implement an attached property that takes a property name, finds that property in the DataContext, and sets it to the DependencyObject to which the property is attached.
Implement your PropertySetter idea
My option #2 would look like this:
<DataTemplate TargetType="{x:Type MyViewModel}">
...
<TextBox local:PropertyHelper.SetViewModelToThis="SpecialControl" />
...
</DataTemplate>
The code in the SetViewModelToThis PropertyChangedCallback would get the view model from the DataContext, reflect on it to find the "SpecialControl" property, then set it to the TextBox. Note that the implementation of SetViewModelToThis must take into account the possiblity that DataContext is not set right away, and that it maybe changed requiring the old setting to be removed and a new one made.
First of all, the DataContext of the control should be the ViewModel object and not a property of it. Second, when you TwoWay bind a property of ViewModel to your control, changes in the control's value will update (in your case, 'set') the value of ViewModel's property.

MVVM Tabcontrol change tab

I am developing an mvvm app with wpf. A requirement just got added on to block the user from changing tabs if a textbox has text.
What is the best way to do this completely in the viewmodel? I don't know how to block a tabitem because there is no dependencyobject command in the tabcontrol to tie into, do i need to roll my own tabcontrol and build an ICommand around the SelectionChanged event?
Should I simply (eegad..don't say it) put code in the code behind of the view in the SelectionChanged event?
Do I have an alternative that I haven't thought of?
You might consider binding each of the TabItems' IsEnabled property to a property in your ViewModel (e.g. ViewModel.TabsEnabled) and set that property to False when the textbox has text. That way, you'll be able to enable/disable those tabs from your ViewModel without having to have a code behind file for that particular view.
Which means you'll have something like the following in your view (assuming your ViewModel is a static class named ViewModel):
<TabItem IsEnabled="{Binding Source={x:Static local:ViewModel.TabsEnabled}}"/>
Then you just have to set the TabsEnabled property on the ViewModel when one of the textboxes has content; there are a couple of ways to do this, but if they are bound to your ViewModel you should have plenty of opportunities to listen for changes and set TabsEnabled as appropriate.

Resources