TwoWay binding of WebBrowser Source property - wpf

I use System.Windows.Controls.WebBrowser in my WPF, MVVM application.
I need to let ViewModel of WebBrowser know when Source property of WebBrowser changed, respectively when loaded html document changed. Of course, the best way is binding of Source property to ViewModel, but I found just this solution for binding, which provides only one-way binding from ViewModel to WebBrowser: databind the Source property of the WebBrowser in WPF
Old WebBrowser from namespace System.Windows.Forms has such a useful methods like "OnDocumentTitleChanged", but I can not find anything similar in System.Windows.Controls.WebBrowser.

Try listening to WebBrowser.Navigated & WebBrowser.Navigating and send a Message in code behind to you VM with the WebBrowser.Source and listen to it on your VM and process it unless you want to go hardcore MVVM, a EventToCommand on one of the 2 events are possible to.

Related

How to bind WPF-Datagrid Control in Editor

I am coming from programming with WinForms and now start to change into WPF. In WinForms I could easily bind a DataGrid before Runtime, to adjust the columns without coding everything.
The WPF-Datagrid has the property "ItemsSource", but I don't understand how to bind it in editor. I have already a DataSource which refers to a SQL-Database, but it will not be shown in the property window.
How to do this?
Screenshot
you have to expose your collection as a public property
make it visible in the xaml (namespace include)
set up your DataContext correctly
create the binding in XAML and set up the columns, like here
If you want to see the changes in your collection online, you have to use ObservableCollection<>

WPF Prism Unity Container. Setting keyboard focus on usercontrol or usercontrol grid parent

I'm working on a project that utilizes WPF, using the Prism library and Unity Container.
I have a mainwindow, which contains a mainviewmodel control which in turn is populated by a collection of user controls.
My goal is to set keyboard focus to the mainviewmodel where I have an EventTrigger InvokeCommandAction which reacts to keyeventsargs...
Currently the only way the command is fired if I use a textbox within the control (or child controls). My goal is to have the mainviewmodel control or grid get and preserve keyboard focus.
Any tips would be appreciated!
//Nathan
Either not understanding your question correctly or you should review the basic concepts of MVVM in a WPF implementation.
The View is the WPF controls.
WPF Window/UserControl files contain WPF markup which is the View.
Controls in a view leverage DataBindings to the DataContext property of either the control itself or the parent containing control (which it will inherit).
DataContext property is set to an instance of an object that is the ViewModel. It contains properties to hold values and commands to execute actions.
So conceptually there is no "mainviewmodel control", there is a MainView which contains controls and may in this case have its DataContext property set to an instance o MainViewModel. (hence my confusion)
Finally, while it is possible and some might even recommend writing UI rules/logic in a view model I haven't found much benefit in it.
You are much better off putting UI logic in the XAML or in the MinView code behind. You can still access the MainViewModel in the code behind by casting the MainView.DataContext property as a MainViewModel.
So for example:
MainView.KeyDown event can be wired up to call MainViewModel.CommandX.Execute();

Correct MVVM design patterns - ViewModel and DataContext

I started playing around with Windows Phone development. I am using MVVM ligth and I am trying to follow good practices. But lately I ran into an issue:
Is it okay to use ViewModel inside a ViewModel as a property?
My scenario is that I am having a MainViewModel which is a Panorama object. This Panorama objects consists of few PanaoramaItems (Notes, Calender and About Controls/Pages).
My question is, if I have for the MainPage.xaml a MainViewModel, is it okay if I put the other ViewModels as properties (NotesViewModel, CalenderViewModel, AboutViewModel) to the MainViewModel, in that case those will inherit from the MainPage the DataContext and I would just bind to a property of the MainViewModel. Or should I rather use the locator pattern to allow the other pages/control to get their own ViewModels and do not inherit the DataContext?
Is it okay, if a control has a ViewModel or should it be rather for pages?
If the Parent and Child ViewModels are related: sure, that is fine, it does not violate the pattern.
This setup allows you to re-use ViewModels and Views across pages and controls.

How to manipulate WPF window controls (tabs, textboxes, listboxes) from a ViewModel

I'm kinda new to WPF. I'm making an app using WPF (all the UI controls are already fixed) and MVVM but most of the events are in the code-behind. I'm in the process of clearing the code-behind but I have codes like (the ones below) to switch through tabs, and to trigger visibility of controls depending on parameters:
tabItem1.Selected = true;
textBox1.Visibility = Visibility.Hidden;
lbxHusbandsWives.Items.Add(txtHusbandsWives.Text + '/' +
cbxHusbandsWivesCountry.Text + '/' +
dpHusbandsWives.SelectedDate.Value.ToShortDateString());
How can I do that in the viewmodel? Well, inside a Command? So I can clear the messy code-behind? Thanks for your help. :)
As I said many times before, MVVM doesn't mean "no code behind". There are things that you can or should do in code-behind, as long as they are strictly related to the view and are not necessary for the ViewModel to work properly.
That being said, in most cases you don't need to do anything in code-behind. You normally use bindings to control the view from the ViewModel. This allows the ViewModel to be completely ignorant of the view: it just exposes properties that the view can access, and sends notifications when the values of the properties change. The ViewModel should definitely not manipulate the view or its components.
Everything in the code you posted can be done with bindings in XAML:
textBox1.Visibility can be bound to a bool property of the ViewModel, using a BooleanToVisibilityConverter
lbxHusbandsWives.ItemsSource can be bound to an ObservableCollection in the ViewModel (an ObservableCollection notifies the view when items are added to or removed from it)
txtHusbandsWives.Text, cbxHusbandsWivesCountry.Text and dpHusbandsWives.SelectedDate can also be bound to properties of the appropriate type

Silverlight: How to bind to a resource that is defined as a usercontrol resource in code

I have a string value that i have defined as a resource in a user controls' constructor
public void usercontrolClas()
{
this.Resources.Add("stringState", "foo");
}
I am trying to access this string as a StaticResource in the XAML of a child usercontrol of the above usercontrol.
<Button Content={.... ,ConverterParameter={StaticResource stringState}"/>
However this doesnt work. Its working if i add the resource to Application.Resources, but I have some state thats is instance specific to the usercontrol and hence has to be scoped to the usercontrol. Anyde
{StaticResource} won't work here. Once you bind it using {StaticResource} changes in resource won't be reflected. And in your case, the resource does not even exist until you create it after UserControl's InitializeComponent() method.
To make this work, you will need {DynamicResource}. But, unfortunately, it is not available in WP7 Silverlight right now.
Other way you could go is creating an Attached Property or a Custom Behavior to work around this limitation.
Tutorials on Behaviors:
Silverlight Behaviors and Triggers
Using Behaviors to enable MouseWheel in Full Screen and Out-of-browser
Silverlight 3 Drag Behavior

Resources