I'm very new to WPF. I'm currently doing a code to detect joints coordinate using the Kinect SDK and displaying on a simple textbox in WPF. The code to detect joints are in a private void Window_Loaded(object sender, RoutedEventArgs e) method. To display the coordinates, I used DataContext. Without further ado, let's just see the XAML code:
<Window x:Class="Prototype.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="480" Width="640">
<Grid>
<TextBox x:Name="coordinateText" Width="150" Height="20" Margin="441,409,27,12" Text="{Binding Path=xInfo}"/>
</Grid>
And this is my C# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Research.Kinect.Nui;
using Coding4Fun.Kinect.Wpf;
namespace Prototype
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
//this.DataContext = new Coordinate { xInfo = "5" };
}
Runtime nui = new Runtime();
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.DataContext = new Coordinate { xInfo = "5" };
nui.Initialize(RuntimeOptions.UseSkeletalTracking); //code for detecting joints
//some code for detecting joints
}
public class Coordinate
{
public string xInfo { get; set; }
public string yInfo { get; set; }
public string zInfo { get; set; }
}
}
}
The thing is the information will not be loaded in the textbox if this.DataContext = new Coordinate { xInfo = "5" }; is not placed in the MainWindow. I have to put it in the Window_Loaded method. Any solutions?
As Coder323 said When window is loaded you need tell WPF TextBox that the Value of the variable xInfo is changed so you should use INotifyPropertyChanged in your Model Class
then where ever you change theValue of your Object it will pick up the changed Value... also
Just Set the DataContext=myCordinate in the Window Constructor then, make my cordinate a variable in the window class.
public class Coordinate : INotifyPropertyChanged
{
private string xInfo;
public string XInfo {
get{retun value};
set{
xInfo=Value;
FirePropertyChanged("XInfo")
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void FirePropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
{
handler.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Do this for other properties and now you can set the value of myCordinate.XInfo="what ever you like" in any event it will notify to your view that the respective property has changed..
I am putting my complete solution here
My Coordinate class
public class Coordinates : INotifyPropertyChanged
{
private string xInfo;
#region Implementation of INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
public string XInfo
{
get { return xInfo; }
set
{
xInfo = value;
InvokePropertyChanged(new PropertyChangedEventArgs("XInfo"));
}
}
public void InvokePropertyChanged(PropertyChangedEventArgs e)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, e);
}
#endregion
}
My Xaml
<Window x:Class="TestApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Grid>
<TextBox Text="{Binding Path=XInfo}" Height="30" Widht="100"></TextBox>
</Grid>
My Xaml.cs
namespace TestApp
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private Coordinates myCoordinates;
public MainWindow()
{
myCoordinates=new Coordinates();
this.DataContext = myCoordinates;
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
myCoordinates.XInfo = "Acbd";
}
}
}
And yes this test Project i made... is working
This might Help :)
Related
I want to bind some properties (FooClass.FooString) of a custom class (FooClass) to my MainWindow. Now below (Working known behavior) is the default working solution if binding some data to a gui.
What I want to do is in the second code block (Not working, but desired behavior). Expose some properties of another class objectto the gui and update it.
**Problem**: TheTestStringis not getting updated (on the gui, code behind works). ThePropertyChangedeventis alsonull` (not subscribed?!).
Is this the wrong way how to bind data?
If I bind the complete FooClass object to the gui and set Path (of TextBlock) to Foo.FooString, the gui and string is updated. But I don't want to do it this way.
Is this the way how to solve it?
Working known behavior
public partial class MainWindow : Window
{
public FooClass Foo { get; } = new FooClass();
public MainWindow()
{
DataContext = this;
InitializeComponent();
Loaded += _OnLoaded;
}
private async void _OnLoaded(object sender, RoutedEventArgs e)
{
await Task.Delay(1000);
Foo.ChangeTheProperty();
}
}
public class FooClass : INotifyPropertyChanged
{
public string FooString
{
get => _FooString;
set
{
if (_FooString == value) return;
_FooString = value;
OnPropertyChanged();
}
}
private string _FooString = "empty";
public void ChangeTheProperty()
{
FooString = "Loaded";
}
// ##############################################################################################################################
// PropertyChanged
// ##############################################################################################################################
#region PropertyChanged
/// <summary>
/// The PropertyChanged Eventhandler
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Raise/invoke the propertyChanged event!
/// </summary>
/// <param name="propertyName"></param>
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
MainWindow.xaml
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance local:MainWindow}"
Title="MainWindow" Height="450" Width="800">
<Grid>
<TextBlock Text="{Binding Path=Foo.FooString}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Grid>
</Window>
Not working, but desired behavior
public partial class MainWindow : Window
{
public string TestString => _Foo.FooString;
private readonly FooClass _Foo;
public MainWindow()
{
_Foo = new FooClass();
DataContext = this;
InitializeComponent();
Loaded += _OnLoaded;
}
private async void _OnLoaded(object sender, RoutedEventArgs e)
{
await Task.Delay(1000);
_Foo.ChangeTheProperty();
}
}
public class FooClass : INotifyPropertyChanged
{
public string FooString
{
get => _FooString;
set
{
if (_FooString == value) return;
_FooString = value;
OnPropertyChanged();
}
}
private string _FooString = "empty";
public void ChangeTheProperty()
{
FooString = "Loaded";
}
// ##############################################################################################################################
// PropertyChanged
// ##############################################################################################################################
#region PropertyChanged
/// <summary>
/// The PropertyChanged Eventhandler
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Raise/invoke the propertyChanged event!
/// </summary>
/// <param name="propertyName"></param>
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
MainWindow.xaml
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance local:MainWindow}"
Title="MainWindow" Height="450" Width="800">
<Grid>
<TextBlock Text="{Binding Path=TestString}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Grid>
</Window>
Solution 1
Subscribe to the Foo.PropertyChanged event and route it to MainWindow.PropertyChanged.
public partial class MainWindow : Window, INotifyPropertyChanged
{
public FooClass Foo { get; } = new FooClass();
public MainWindow()
{
Foo.PropertyChanged += (sender, args) => OnPropertyChanged(args.PropertyName);
DataContext = this;
InitializeComponent();
Loaded += _OnLoaded;
}
private async void _OnLoaded(object sender, RoutedEventArgs e)
{
await Task.Delay(1000);
Foo.ChangeTheProperty();
}
// ##############################################################################################################################
// PropertyChanged
// ##############################################################################################################################
#region PropertyChanged
/// <summary>
/// The PropertyChanged Eventhandler
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Raise/invoke the propertyChanged event!
/// </summary>
/// <param name="propertyName"></param>
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
I might not have fully understood what you want, but here is a working example of data binding, that is somewhat close to your example.
The two main changes were:
Set the datacontext to the VM and not the code behind
Actually give OnPropertyChanged the argument it needs to correctly trigger the refresh, the name of the property.
Result:
MainWindow.xaml
<Window x:Class="ListViewColor.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<TextBlock Text="{Binding Foo.FooString}" VerticalAlignment="Center" HorizontalAlignment="Center" Background="Aqua"/>
</Grid>
</Window>
MainWindow.xaml.cs
namespace ListViewColor
{
public partial class MainWindow : Window
{
public FooClass Foo { get; } = new FooClass();
public MainWindow()
{
DataContext = this;
InitializeComponent();
Loaded += _OnLoaded;
}
private async void _OnLoaded(object sender, RoutedEventArgs e)
{
await Task.Delay(1000);
Foo.ChangeTheProperty();
}
}
}
FooClass.cs
using System.ComponentModel;
using System.Runtime.CompilerServices;
public class FooClass : INotifyPropertyChanged
{
private string _FooString = "Empty";
public string FooString
{
get
{
return _FooString;
}
set
{
if (_FooString == value) return;
_FooString = value;
OnPropertyChanged();
}
}
public void ChangeTheProperty()
{
FooString = "Loaded";
}
#region PropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
I hope that helps!
I'm simply creating a Observable collection in Background and binding to it.
All objects inherit from INotifyPropertyChanged.
but nevertheless the Memory consumption is continuously raising.
The following Objects instances are continuously raising
WeakReference
FrugalObjectList<WeakEventManager+Listener>
ConditionalWeakTable<TKey, TValue>+Entry<Object, Object>[]
WeakEventTable+EventKey
ConditionalWeakTable<Object, Object>
SingleItemList<WeakEventManager+Listener>
Object
Int32[]
WeakEventManager+ListenerList<NotifyCollectionChangedEventArgs>
WeakEventManager+ListenerList<PropertyChangedEventArgs>
HybridDictionary
ListDictionary
ListDictionary+DictionaryNode
WeakEventManager+ListenerList<EventArgs>
WeakEventManager+ListenerList<CurrentChangingEventArgs>
CollectionRecord
I'm using .net 4.5.2.
See also the following Screenshots:
MemoryConsumptionOverview
ClassesIncreasing
Attached the sample code
XAML Markup:
<Window x:Class="BindingDataGridTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="120" Width="200"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Grid>
<DataGrid ItemsSource="{Binding BindingVals, Mode=OneWay}" />
</Grid>
</Window>
Code behind:
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Threading;
namespace BindingDataGridTest
{
public partial class MainWindow : INotifyPropertyChanged
{
public ObservableCollection<Values> BindingVals { get; set; }
public MainWindow()
{
BindingVals = new ObservableCollection<Values>();
InitializeComponent();
DispatcherTimer myTimer = new DispatcherTimer { Interval = new TimeSpan(20) };
myTimer.Tick += CreateVals;
myTimer.Start();
}
private void CreateVals(object sender, EventArgs e)
{
Values myMainval = new Values
{
MyVal = "1V" + new Random().Next()
};
BindingVals.Clear();
BindingVals.Add(myMainval);
GC.Collect();
}
public event PropertyChangedEventHandler PropertyChanged;
}
public class Values : INotifyPropertyChanged
{
public string MyVal { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
}
}
It's caused by DispatcherTimer.
A DispatcherTimer will keep an object alive whenever the object's methods are bound to the timer.
https://msdn.microsoft.com/ru-ru/library/system.windows.threading.dispatchertimer(v=vs.110).aspx
Use System.Timers.Timer instead and Dispatcher separately.
public MainWindow()
{
BindingVals = new ObservableCollection<Values>();
InitializeComponent();
System.Timers.Timer myTimer = new Timer {Interval = 20};
myTimer.Elapsed += CreateVals;
myTimer.Start();
}
private void CreateVals(object sender, EventArgs e)
{
Dispatcher.Invoke(() =>
{
Values myMainval = new Values
{
MyVal = "1V" + new Random().Next()
};
BindingVals.Clear();
BindingVals.Add(myMainval);
GC.Collect();
});
}
I had a Model class named as FormModel.cs while coding WPF using C#..
Below is my code. I am clueless that even though debugger is going into setter method of Name, PropertyChanged flag is not raising.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
namespace MVVM.Models
{
public class FormModel : INotifyPropertyChanged
{
private string _name;
public FormModel()
{
_name = "";
_bColor = "";
_fColor = "";
}
public string Name
{
get { return _name; }
set
{
_name = value;
NotifyPropertyChanged("Name");
}
}
private string _bColor;
public string BColor
{
get { return _bColor; }
set { _bColor = "RED"; }
}
private string _fColor;
public string FColor
{
get { return _fColor; }
set { _fColor = "BLUE"; }
}
public void apply(string Name, string BColor, string FColor)
{
this.Name = Name;
this.BColor = BColor;
this.FColor = FColor;
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propName)
{
if(this.PropertyChanged!=null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
if(propName=="Name")
{
MessageBox.Show("Hi" + Name);
}
}
}
}
}
Here are remaining Files I edited in question in quest on request
MainViewModel.cs
using MVVM.Models;
using MVVM.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Windows.Input;
using System.Windows;
namespace MVVM.ViewModels
{
public class MainViewModel : ViewModelBase
{
private FormModel FModel;
private ICommand refresh;
public ICommand Refresh
{
get { return refresh; }
set { refresh = value; }
}
public MainViewModel()
{
FModel = new FormModel();
string s = "Pratik";
object o=(object)s;
refresh = new RelayCommand(new Action<object>(getGreet));
}
public void getGreet(object s1)
{
FModel.apply("dsf", "sf", "sf");
MessageBox.Show(s1.ToString());
}
private string _name;
public string Name
{
get { return _name; }
set {
_name = value;
FModel.Name = value;
}
}
}
class RelayCommand : ICommand
{
private Action<object> _action;
public RelayCommand(Action<object> action)
{
_action = action;
}
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
_action(parameter);
}
}
}
ViewModelBase.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
namespace MVVM.ViewModels
{
public abstract class ViewModelBase: INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if(handler!=null)
{
if (propertyName == "Name")
{
//Command
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
}
Below are View Files
MainView.xaml
<Window x:Class="MVVM.Views.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:VM="clr-namespace:MVVM.ViewModels"
Title="UI" Height="300" Width="300">
<Grid Background="RED">
<TextBox Name="TT" TextWrapping="Wrap" Text="{Binding Name}" Margin="20,15,160,225" />
<TextBox TextWrapping="Wrap" Text="Back Color" Margin="20,73,195,167" />
<TextBox TextWrapping="Wrap" Text="Font Color" Margin="20,122,195,118"/>
<Label Content="getGreet" HorizontalAlignment="Left" Margin="187,90,0,0" VerticalAlignment="Top"/>
<Button Content="Finish" Command="{Binding Refresh}" HorizontalAlignment="Left" Margin="112,163,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>
</Window>
MainView.xaml.cs
//using MVVM.Models;
using MVVM.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace MVVM.Views
{
/// <summary>
/// Interaction logic for UI.xaml
/// </summary>
public partial class MainView : Window
{
public MainView()
{
InitializeComponent();
this.DataContext = new MainViewModel();
}
}
}
And Main Files...
App.xaml
<Application x:Class="MVVM.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Startup="OnStartup">
<Application.Resources>
</Application.Resources>
</Application>
App.xaml.cs
using MVVM.ViewModels;
using MVVM.Views;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
namespace MVVM
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
public void OnStartup(object sender, StartupEventArgs e)
{
// Create the ViewModel and expose it using the View's DataContext
MainView view = new MainView();
view.DataContext = new MainViewModel();
view.Show();
}
}
}
Please help.
You are binding to the Name property of your MainViewModel not the Name property of your Model. Even thought this is essentially a passthrough to the Model.Name property, the binding is attaching to the PropertyChanged event of the MainViewModel which doesn't get raised in your setter.
Add an OnPropertyChanged call in the setter of your MainViewModel.Name property and it should work.
public string Name
{
get { return _name; }
set
{
_name = value;
FModel.Name = value;
OnPropertyChanged("Name");
}
}
How to disable all the properties or some of the properties PropertyChanged event for some time when we are using INotifypropertyChanged?
In order for INotifyPropertyChanged to work, you need to raise the PropertyChanged event. Therefore, to make it not work, you just don't raise that event.
Here's a small example class:
public class NPCExample : INotifyPropertyChanged
{
public NPCExample()
{
}
private string mSomeProperty = "Set Property";
public string SomeProperty
{
get { return mSomeProperty; }
set
{
mSomeProperty = value;
if (mUseNotifyPropertyChanged)
NotifyPropertyChanged("SomeProperty");
}
}
private Boolean mUseNotifyPropertyChanged = true;
public Boolean UseNotifyPropertyChanged
{
get { return mUseNotifyPropertyChanged; }
set
{
mUseNotifyPropertyChanged = value;
NotifyPropertyChanged("UseNotifyPropertyChanged");
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
In this class, each property calls the common "NotifyPropertyChanged" method for raising the PropertyChanged event. There is an additional variable defined (here, I used a public Property so I could bind it to a checkbox) that tells whether or not to raise the event, as used in the SomeProperty event.
Here's a small, quick-n-dirty program to show this in action:
XAML
<Window x:Class="MyNamespace.SelectiveNotifyPropertyChanged"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="SelectiveNotifyPropertyChanged" Height="300" Width="300">
<StackPanel>
<TextBlock Text="{Binding SomeProperty}" />
<CheckBox x:Name="chkINPCEnabled"
Content="Enable INotifyPropertyChanged"
IsChecked="{Binding UseNotifyPropertyChanged}"></CheckBox>
<StackPanel Orientation="Horizontal">
<TextBox x:Name="txtIsProperty"
Text="Set Property" />
<Button x:Name="btnSetProperty"
Content="Set Property" />
</StackPanel>
</StackPanel>
</Window>
Code Behind
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace MyNamespace
{
/// <summary>
/// Interaction logic for SelectiveNotifyPropertyChanged.xaml
/// </summary>
public partial class SelectiveNotifyPropertyChanged : Window
{
public SelectiveNotifyPropertyChanged()
{
InitializeComponent();
NPCExample example = new NPCExample();
this.DataContext = example;
btnSetProperty.Click +=
(s, e) => example.SomeProperty = txtIsProperty.Text;
}
}
public class NPCExample : INotifyPropertyChanged
{
public NPCExample()
{
}
private string mSomeProperty = "Set Property";
public string SomeProperty
{
get { return mSomeProperty; }
set
{
mSomeProperty = value;
if (mUseNotifyPropertyChanged)
NotifyPropertyChanged("SomeProperty");
}
}
private Boolean mUseNotifyPropertyChanged = true;
public Boolean UseNotifyPropertyChanged
{
get { return mUseNotifyPropertyChanged; }
set
{
mUseNotifyPropertyChanged = value;
NotifyPropertyChanged("UseNotifyPropertyChanged");
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
}
If you are referring to a binding, you can set the UpdateSourceTrigger to Explicit, which means any changes won't get saved until you explicitly tell it to update
<TextBox Text="{Binding SomeValue, UpdateSourceTrigger=Explicit}" />
Based on your comment to Rachel it sounds like you might want to set the private property backing member sometimes. Could you expose a public method in your underlying class that would set the private member but not call NotifyPropertyChaged?
Public Class SomeClass
... define property SomeProp and m_SomeProp
Public Sub SetSomeProp(val as string)
m_SomePreop=val
End Sub
End Class
I'm just playing around with WPF and MVVM, and I have made a simple app that displays a Rectangle that changes color whenever Network availability changes.
But when that happens, I get this error: Cannot use a DependencyObject that belongs to a different thread than its parent Freezable.
Code
XAML
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="400" Width="600">
<DockPanel LastChildFill="True">
<Rectangle x:Name="networkStatusRectangle" Width="200" Height="200" Fill="{Binding NetworkStatusColor}" />
</DockPanel>
</Window>
Code-behind
using System.Windows;
using WpfApplication1.ViewModels;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
DataContext = new NetworkViewModel();
}
}
}
ViewModel
using System.ComponentModel;
using System.Net.NetworkInformation;
using System.Windows.Media;
namespace WpfApplication1.ViewModels
{
public class NetworkViewModel : INotifyPropertyChanged
{
private Brush _NetworkStatusColor;
public Brush NetworkStatusColor
{
get { return _NetworkStatusColor; }
set
{
_NetworkStatusColor = value;
NotifyOfPropertyChange("NetworkStatusColor");
}
}
public NetworkViewModel()
{
NetworkChange.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(NetworkChange_NetworkAvailabilityChanged);
}
protected void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
{
if (e.IsAvailable)
{
this.NetworkStatusColor = new SolidColorBrush(Colors.Green);
}
else
{
this.NetworkStatusColor = new SolidColorBrush(Colors.Red);
}
}
public event PropertyChangedEventHandler PropertyChanged = delegate { };
public void NotifyOfPropertyChange(string propertyName)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
I assume that I should change the NetworkStatusColor property by invoking something?
You assume correctly. It's the Dispatcher class and the .Invoke method you want to take a look at.
Something a bit like this:
if (this.Dispatcher.Thread != Thread.CurrentThread)
{
this.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(...your method...), any, params, here);
return
}
There's an MSDN article here with some more info.
With MVVM you have a couple of options when dealing with dispatching. Either you can send some kind of message to your view to have it invoke the operation for you, or you can create some kind of abstract dispatcher service that you are able to easily mock.
Take a look at the MVVM Light toolkit, as it includes a simple dispatcher-service you can use/copy.