This mode is very puzzling and the less used mode out of all modes of RelativeSource modes. It is used for discrete cases. The agenda of this method is to link the value of any control to the new one. For example, if we have to use the value of textbox in any other textbox or text block, we can use PreviousData mode. And from this, we get that we have to use this mode with the item controls.
Example:
Firstly, create the ItemsControl using the custom collection.
class Items : ObservableCollection-
{
public Items()
{
Add(new Item { Value = 110.30 });
Add(new Item { Value = 200 });
Add(new Item { Value = 70.89 });
Add(new Item { Value = 123.45 });
Add(new Item { Value = 222.00 });
Add(new Item { Value = 50.50 });
}
}
In this, we use ObservableCollection of Item type. It has only one value of type double.
Now, we create the class for Item.
namespace RelativeSrc
{
class Item : INotifyPropertyChanged
{
private double _value;
public double Value
{
get { return _value; }
set { _value = value; OnPropertyChanged("Value"); }
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string PropertyName)
{
if (null != PropertyChanged)
{
PropertyChanged(this,
new PropertyChangedEventArgs(PropertyName));
}
}
}
}
Now, for binding the ItemsControl to the collection of data, we set constructor level collection of DataContext property to the whole Document
public partial class PreviousDataProp: Window
{
public PreviousDataProp()
{
InitializeComponent();
this.DataContext = new Items();
}
}
Now by adding the ItemsControl with the Binding property and we can observe bit improvement with the actual view of the ItemsControl.
This code only shows the actual present data specified in the Items collection. To get the previous data of the collection we have to add one textblock having the PreviousData property. And in this textblock, we add the value of previous border value to the items list control and through this, we get the actual output as visualized.
Image: Previous Data Property-Output
Here, we can see the value of the previous item is displayed in the new text block which we added recently.