I have a simple viewmodel class which contains three properties.
public class ColorViewModel : INotifyPropertyChanged
{
private int red;
public int Red
{
get { return red; }
set
{
if (red != value)
{
red = value;
RaisePropertyChanged("Red");
}
}
}
private int green;
public int Green
{
get { return green; }
set
{
if (green != value)
{
green = value;
RaisePropertyChanged("Green");
}
}
}
private int blue;
public int Blue
{
get { return blue; }
set
{
if (blue!= value)
{
blue = value;
RaisePropertyChanged("Blue");
}
}
}
public event PropertyChangedEventHandler PropertyChanged = delegate { };
private void RaisePropertyChanged(string propName)
{
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
Now I want to pass entire model to a given converter in order to create a color for RGB values.
<Rectangle Grid.Row="4" Grid.Column="1" HorizontalAlignment="Center" Height="120" Width="120"
Fill="{Binding Model, Converter={StaticResource intToBrushValueConverter}}"/>
where Model is one instance of my ColorViewModel.
The problem is that intToBrushValueConverter converter is trigger only once, when the program started. With the other words, the converter is not fired when Model is changed.
Then when one property of ColorViewModel is changed, the PropertyChanged is working, but initToBrushValueConverter converter it's not hitting.
There is one method to fix that ?
I'm searching for one solution without using MultiBinding or CommandParameter.
Thanks in advance.
Hook property changed event to parent viewmodel and in fire property changed event whenever property changes on Color property.
public class ParentViewModel : INotifyPropertyChanged
{
private ColorViewModel color;
public ColorViewModel Color
{
get { return color; }
set
{
if (color != value)
{
if (color != null)
color.PropertyChanged -= this.ChildPropertyChanged;
color = value;
if (color != null)
color.PropertyChanged += this.ChildPropertyChanged;
RaisePropertyChanged("Color");
}
}
}
private void ChildPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if(Color == sender)
{
RaisePropertyChanged("Color");
}
}
public event PropertyChangedEventHandler PropertyChanged = delegate { };
private void RaisePropertyChanged(string propName)
{
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
UPDATE:
Full working solution below.
MainWindows.xaml.cs
using System;
using System.Windows;
namespace WpfApp1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private Random random = new Random();
private ParentViewModel model;
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
this.model = new ParentViewModel();
this.model.Color = new ColorViewModel();
}
public ParentViewModel Model
{
get { return model; }
set
{
model = value;
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.model.Color.Blue = (byte)random.Next(0, 255);
this.model.Color.Green = (byte)random.Next(0, 255);
this.model.Color.Red = (byte)random.Next(0, 255);
}
}
}
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"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:IntToBrushConverter x:Key="intToBrushConverter" />
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="50" />
</Grid.RowDefinitions>
<Rectangle HorizontalAlignment="Center" Height="120" Width="120" Fill="{Binding Model.Color, Converter={StaticResource intToBrushConverter}, FallbackValue=Black}"/>
<Button Grid.Row="1" Content="Click me!" Click="Button_Click" />
</Grid>
</Window>
IntToBrushConverter.cs
using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Media;
namespace WpfApp1
{
public class IntToBrushConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if(value is ColorViewModel)
{
var color = value as ColorViewModel;
return new SolidColorBrush(Color.FromRgb(color.Red, color.Green, color.Blue));
}
throw new NotSupportedException();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
}
ParentViewModel.cs
using System.ComponentModel;
namespace WpfApp1
{
public class ParentViewModel : INotifyPropertyChanged
{
private ColorViewModel color;
public ColorViewModel Color
{
get { return color; }
set
{
if (color != value)
{
if (color != null)
color.PropertyChanged -= this.ChildPropertyChanged;
color = value;
if (color != null)
color.PropertyChanged += this.ChildPropertyChanged;
RaisePropertyChanged("Color");
}
}
}
private void ChildPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (Color == sender)
{
RaisePropertyChanged("Color");
}
}
public event PropertyChangedEventHandler PropertyChanged = delegate { };
private void RaisePropertyChanged(string propName)
{
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
}
ColorViewModel.cs
using System.ComponentModel;
namespace WpfApp1
{
public class ColorViewModel : INotifyPropertyChanged
{
private byte red;
public byte Red
{
get { return red; }
set
{
if (red != value)
{
red = value;
RaisePropertyChanged("Red");
}
}
}
private byte green;
public byte Green
{
get { return green; }
set
{
if (green != value)
{
green = value;
RaisePropertyChanged("Green");
}
}
}
private byte blue;
public byte Blue
{
get { return blue; }
set
{
if (blue != value)
{
blue = value;
RaisePropertyChanged("Blue");
}
}
}
public event PropertyChangedEventHandler PropertyChanged = delegate { };
private void RaisePropertyChanged(string propName)
{
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
}
App.xaml and App.xaml.cs are standard as generated by template.
Output
Hope it helps!
The problem is that the Rectangle listens only to PropertyChanged events for the Model and not for the three color properties.
Try raising an event for the Model itself using RaisePropertyChanged("Model"); or RaisePropertyChanged(null);.
Related
How can I search for a row by a column and later select that row in a XamDataGrid.
I've tried iterating over the DataSource, but the type of the elements isn't very helpful, it has only a HasData bool property exposed.
Try to use XamDataGrid.FieldLayouts.DataPresenter.Records collection and check for the required cell. When the record is found it can be selected by setting record.IsSelected = true;
Something like that:
using System;
using System.Windows;
using System.Windows.Media;
using System.Collections.ObjectModel;
using System.ComponentModel;
using Infragistics.Windows.DataPresenter;
namespace IGFindRow
{
public partial class MainWindow : Window
{
public MainWindow()
{
_cars = Cars;
InitializeComponent();
}
#region Code fragment from samples provided by Infragistics
public ObservableCollection<Car> _cars = null;
public ObservableCollection<Car> Cars
{
get
{
if (this._cars == null)
{
this._cars = new ObservableCollection<Car>();
this._cars.Add(new Car("Dodge", "Ram", Colors.Blue, 22050.00, 153));
this._cars.Add(new Car("Ford", "Explorer", Colors.Green, 27175.00, 96));
this._cars.Add(new Car("BMW", "Z4", Colors.Silver, 35600.00, 42));
this._cars.Add(new Car("Toyota", "Camry", Colors.Black, 20790.99, 131));
}
return _cars;
}
}
public class Car : INotifyPropertyChanged
{
string m_make;
string m_model;
Color m_color;
double m_baseprice;
int m_milage;
public Car(string make, string model, Color color, double baseprice, int milage)
{
this.Make = make;
this.Model = model;
this.Color = color;
this.BasePrice = baseprice;
this.Milage = milage;
}
public string Make
{
get { return m_make; }
set
{
if (m_make != value)
{
m_make = value;
NotifyPropertyChanged("Make");
}
}
}
public string Model
{
get { return m_model; }
set
{
if (m_model != value)
{
m_model = value;
NotifyPropertyChanged("Model");
}
}
}
public Color Color
{
get { return m_color; }
set
{
if (m_color != value)
{
m_color = value;
NotifyPropertyChanged("Color");
}
}
}
public double BasePrice
{
get { return m_baseprice; }
set
{
if (m_baseprice != value)
{
m_baseprice = value;
NotifyPropertyChanged("BasePrice");
}
}
}
public int Milage
{
get { return m_milage; }
set
{
if (m_milage != value)
{
m_milage = value;
NotifyPropertyChanged("Milage");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(info));
}
}
#endregion
private void Search_Click(object sender, RoutedEventArgs e)
{
// Enumerate records
foreach (var it in dataGrid.FieldLayouts.DataPresenter.Records)
{
if (it is DataRecord record)
{
// Check the current column value
if (record.Cells["Make"].Value.ToString().ToUpper() == Maker.Text.ToUpper())
{
record.IsSelected = true;
break;
}
}
}
}
}
}
The XAML:
<Window x:Class="IGFindRow.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:ig="http://infragistics.com/DataPresenter"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800"
Name="dgTest">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal" Margin="5,5,5,5">
<Label Name="ColumnName" Padding="5,5,0,5">Maker:</Label>
<TextBox Name="Maker" Padding="5,5,5,5" Margin="3,0,20,0">Ford</TextBox>
<Button Name="Search" Padding="5,5,5,5" Click="Search_Click">Press to search</Button>
</StackPanel>
<ig:XamDataGrid x:Name="dataGrid" Grid.Row="1"
IsGroupByAreaExpanded="False"
GroupByAreaLocation="None"
Theme="Generic"
DataSource="{Binding ElementName=dgTest, Path=Cars}">
<ig:XamDataGrid.FieldLayoutSettings>
<ig:FieldLayoutSettings SelectionTypeRecord="Single" />
</ig:XamDataGrid.FieldLayoutSettings>
<ig:XamDataGrid.ViewSettings>
<ig:GridViewSettings/>
</ig:XamDataGrid.ViewSettings>
<ig:XamDataGrid.FieldSettings>
<ig:FieldSettings CellClickAction="SelectRecord" AllowRecordFiltering="True"/>
</ig:XamDataGrid.FieldSettings>
</ig:XamDataGrid>
</Grid>
</Window>
What is the proper way, for the UI to get notified, that property "Difference" has changed in the following code sample?
The property is read-only. The property's value must always be calculated based on the other properties.
MainWindow.xaml:
<Window x:Name="winCalcs" x:Class="BindingList.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:m="clr-namespace:BindingList"
Title="Calculations" Height="350" Width="525">
<Window.Resources>
<m:OperationList x:Key="OperationData"/>
<CollectionViewSource x:Key="Operations"
Source="{StaticResource ResourceKey=OperationData}"/>
</Window.Resources>
<Grid>
<TabControl x:Name="tabsMain">
<TabItem x:Name="tab01" Header="Tab 1">
<DataGrid x:Name="dg01"
ItemsSource="{Binding
Source={StaticResource ResourceKey=Operations},
UpdateSourceTrigger=PropertyChanged}" />
</TabItem>
<TabItem x:Name="tab02" Header="Tab 2">
<DataGrid x:Name="dg02"
ItemsSource="{Binding
Source={StaticResource ResourceKey=Operations},
UpdateSourceTrigger=PropertyChanged}" />
</TabItem>
</TabControl>
</Grid>
</Window>
Operation.cs:
namespace BindingList
{
class Operation : INotifyPropertyChanged
{
private float _minuend;
private float _subtrahend;
public float Minuend
{
get
{
return this._minuend;
}
set
{
if (this._minuend == value) return;
this._minuend = value;
this.NotifyPropertyChanged("Minuend");
}
}
public float Subtrahend
{
get
{
return this._subtrahend;
}
set
{
if (this._subtrahend == value) return;
this._subtrahend = value;
this.NotifyPropertyChanged("Subtrahend");
}
}
public float Difference
{
get
{
return Minuend - Subtrahend;
}
private set {}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string p)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(p));
}
}
}
OperationList.cs:
namespace BindingList
{
class OperationList : BindingList<Operation>
{
public OperationList()
{
Add(new Operation());
}
}
}
Difference changes when Minuend or Subtrahend changes. That means you need to notify changed for Difference within the set of Minuend or Subtrahend.
There is no need for property setter for Difference.
On a side note, there is no need for using this everywhere
public float Minuend
{
get
{
return _minuend;
}
set
{
if (_minuend == value) return;
_minuend = value;
NotifyPropertyChanged("Minuend");
NotifyPropertyChanged("Difference");
}
}
public float Subtrahend
{
get
{
return _subtrahend;
}
set
{
if (_subtrahend == value) return;
_subtrahend = value;
NotifyPropertyChanged("Subtrahend");
NotifyPropertyChanged("Difference");
}
}
public float Difference
{
get
{
return Minuend - Subtrahend;
}
}
In these situations I typically set the property explicitly and and raise the PropertyChanged event.
namespace BindingList
{
class Operation : INotifyPropertyChanged
{
private float _minuend;
private float _subtrahend;
private float _difference;
public float Minuend
{
get
{
return this._minuend;
}
set
{
if (this._minuend == value)
return;
this._minuend = value;
this.NotifyPropertyChanged("Minuend");
this.UpdateDifference();
}
}
public float Subtrahend
{
get
{
return this._subtrahend;
}
set
{
if (this._subtrahend == value)
return;
this._subtrahend = value;
this.NotifyPropertyChanged("Subtrahend");
this.UpdateDifference();
}
}
private void UpdateDifference()
{
this.Difference = this.Minuend - this.Subtrahend;
}
public float Difference
{
get
{
return this._difference
}
private set
{
if (this._difference == value)
return;
this._difference = value;
this.NotifyPropertyChanged("Difference");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string p)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(p));
}
}
}
I have a Listbox where it's items are objects. In these objects I store two colors.
I want to bind these colors with an other object's property, but how can I achieve this?
The listbox looks like this:
Listbox1.Items.Add(new ColorAndMoreClass(Color.Red, Color.Blue));
Far away, in an other class there is a property which I'd like to bind to.
How can I do that?
Your rootclass could look like this.
In the class you have a object representing a different Class.
public class ColorAndMoreClass: INotifyPropertyChanged
{
private Color _c;
private Color _c2;
private OtherClass _example;
public ColorAndMoreClass(Color c, Color c2)
{
_c= c;
_c2 = c2;
}
public OtherClass example
{
get { return _example }
set
{
_example = value;
OnPropertyChanged("example");
}
}
public Color c
{
get { return _c; }
set
{
_c= value;
OnPropertyChanged("c");
}
}
public Color c2
{
get { return _c2; }
set
{
_c2 = value;
OnPropertyChanged("c2");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string info)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(info));
}
}
}
Your other class could look like this. I just took a simple string.
public class OtherClass : INotifyPropertyChanged
{
private String _someOtherProperty;
public OtherClass(){}
public String someOtherProperty
{
get { return _someOtherProperty; }
set
{
_someOtherProperty= value;
OnPropertyChanged("someOtherProperty");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string info)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(info));
}
}
}
In your Code behind make a property the Listbox can bind to
public List<ColorAndMoreClass>> ListOfColorAndMore{ get; set; }
public Window1()
{
ListOfColorAndMore = GetDataThatFillsUpTheProperty();
InitializeComponent();
DataContext = this;
}
Your XAML could then look like this. The Datatemplate is used to tell XAML how to display your object.
<Grid>
<ListBox ItemsSource={Binding ListOfColorAndMore}>
<DataTemplate x:Key="myTaskTemplate">
<StackPanel>
<TextBlock Text="{Binding Path=c.R}" />
<TextBlock Text="{Binding Path=c2.R}"/>
<TextBlock Text="{Binding Path=example.someOtherProperty}"/>
</StackPanel>
</DataTemplate>
</ListBox>
</Grid>
I hope it is this that you mean. But your question is not that clear.
I bind a class which derived from INotifyPropertyChange to a Datacontext.
after some interaction, a value will be calculated and output property will be updated.
My problem is that the result textbox didn't update at all.
public partial class setraSubWpfTolerance : UserControl
{
public setraFit objSource = new setraFit();
public setraSubWpfTolerance()
{
InitializeComponent();
this.DataContext = objSource;
}
}
And the class:
public class setraFit : INotifyPropertyChanged
{
private readonly CollectionView _BoreSystems;
public CollectionView BoreSystems
{
get { return _BoreSystems; }
}
private decimal? _MaxBoreDimension;
public decimal? MaxBoreDimension
{
get { return _MaxBoreDimension; }
set
{
if (_MaxBoreDimension == value) return;
_MaxBoreDimension = value;
onPropertyChanged("MaxBoreDimension");
}
}
private string _BoreSystem;
public string BoreSystem
{
get { return _BoreSystem; }
set
{
if (_BoreSystem == value) return;
_BoreSystem = value;
calcBoreDimension();
onPropertyChanged("BoreSystem");
}
}
public setraFit()
{
IList<string> listBore = setraStaticTolerance.getBoreList();
_BoreSystems = new CollectionView(listBore);
}
public event PropertyChangedEventHandler PropertyChanged;
private void onPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private void calcBoreDimension()
{
_MaxBoreDimension = (decimal)100.035;
}
}
Last but not least the XAML
<UserControl x:Class="SetraSubForms.setraSubWpfTolerance"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="375">
<Grid>
<ComboBox Height="23" HorizontalAlignment="Left" Margin="194,10,0,0" Name="BoreSystemComboBox" VerticalAlignment="Top" Width="120"
ItemsSource="{Binding Path=BoreSystems}"
SelectedValue="{Binding Path=BoreSystem}"/>
<TextBox HorizontalAlignment="Left" Margin="194,67,0,37" Name="MaxDimBoreTextBox" Width="120" IsReadOnly="False"
Text="{Binding Path=MaxBoreDimension, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
</Grid>
</UserControl>
I expected to receive the dummy value of 100.035 after changing the combobox but the textbox did not update. If i run step by step i can see the "MaxBoreDimension" property of setraFit is changed.
What did i do wrong?
Thanks in advance for your help
sittingDuck
Your method is updating the private value, not the Property:
private void calcBoreDimension()
{
_MaxBoreDimension = (decimal)100.035;
}
Change to
private void calcBoreDimension()
{
MaxBoreDimension = (decimal)100.035;
}
You're doing the same thing in the constructor, which is causing your calcBoreDimension method to not run:
public setraFit()
{
IList<string> listBore = setraStaticTolerance.getBoreList();
_BoreSystems = new CollectionView(listBore);
}
should be
public setraFit()
{
IList<string> listBore = setraStaticTolerance.getBoreList();
BoreSystems = new CollectionView(listBore); //this line!
}
When you create properties that point to private fields, you should almost never have to set the private field anywhere other than the property. This is why properties exist- so that whenever you get or set them, you will run the code in the get and set blocks instead of just retrieving the current value.
SOLVED!
The key is to initate the PropertyChanged event for the "MaxBoreDimension"
public decimal? NominalDimension
{
get { return _NominalDimension; }
set
{
if (_NominalDimension == value) return;
_NominalDimension = value;
calcBoreDimension();
onPropertyChanged("NominalDimension");
onPropertyChanged("MaxBoreDimension");
}
}
Thanks DLeh for the contribution.
I am using INotifyPropertyChanged but it will give me null when I shaw the PropertyChanged so what i can do..
my code is like this..
public class Entities : INotifyPropertyChanged
{
public Entities(int iCount)
{
_iCounter = iCount;
}
private int _iCounter;
public int iCounter
{
get
{
return _iCounter;
}
set
{
value = _iCounter;
NotifyPropertyChanged("iCounter");
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
Thanks...
I tried putting your code in my program and it is working fine. I am getting the EventArg as the property:
class Program
{
static void Main(string[] args)
{
var ent = new Entities(10);
ent.PropertyChanged += new PropertyChangedEventHandler(ent_PropertyChanged);
ent.iCounter = 100;
}
static void ent_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
throw new NotImplementedException();
}
}
public class Entities : INotifyPropertyChanged
{
public Entities(int iCount)
{
_iCounter = iCount;
}
private int _iCounter;
public int iCounter
{
get
{
return _iCounter;
}
set
{
_iCounter = value;
NotifyPropertyChanged("iCounter");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
What is the exact erro you are getting?
This is i think a bug in INotifyPropertyChanged .
There can be 2 workaround
1st Workaround
1- Assign iCounter property to a UI control like Lable.
2- Now change the value of the property this time , PropertyChanged event will have a reference of your method and will not be null;
2nd workaround
Assign PropertyChanged delegate in the Entities class constructor
i am giving the demo code in WPF
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.Resources>
<ToolTip x:Key="#tooltip">
<TextBlock Text="{Binding CompanyName}"/>
</ToolTip>
</Grid.Resources>
<TextBlock Text="{Binding Name}" Background="LightCoral" />
<Rectangle Width="200" Height="200" Fill="LightBlue" VerticalAlignment="Center" HorizontalAlignment="Center" ToolTip="{DynamicResource #tooltip}" Grid.Row="1"/>
<Button Click="Button_Click" Grid.Row="2" Margin="20">Click Me</Button>
</Grid>
see here CompanyName is assigned to a tool tip.
// this is Window1.Cs file
public Window1()
{
DataContext = DemoCustomer.CreateNewCustomer();
InitializeComponent();
}
// Now DemoCustomer Class
public class DemoCustomer : INotifyPropertyChanged
{
// These fields hold the values for the public properties.
private Guid idValue = Guid.NewGuid();
private string customerName = String.Empty;
private string companyNameValue = String.Empty;
private string phoneNumberValue = String.Empty;
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
// The constructor is private to enforce the factory pattern.
private DemoCustomer()
{
customerName = "no data";
companyNameValue = "no data";
phoneNumberValue = "no data";
}
// This is the public factory method.
public static DemoCustomer CreateNewCustomer()
{
return new DemoCustomer();
}
// This property represents an ID, suitable
// for use as a primary key in a database.
public Guid ID
{
get
{
return this.idValue;
}
}
public string CompanyName
{
get { return this.companyNameValue; }
set
{
if (value != this.companyNameValue)
{
this.companyNameValue = value;
OnPropertyChanged("CompanyName");
}
}
}
public string PhoneNumber
{
get { return this.phoneNumberValue; }
set
{
if (value != this.phoneNumberValue)
{
this.phoneNumberValue = value;
OnPropertyChanged("PhoneNumber");
}
}
}
}
and finally changing the value
private void Button_Click(object sender, RoutedEventArgs e)
{
DemoCustomer dc = this.DataContext as DemoCustomer;
dc.CompanyName = "Temp";
}