Reputation: 709
In my project I wanna change the background of a Datagrid cell whenever a new Bid price comes in. And I set it using
public class Notifier : INotifyPropertyChanged
{
public bool BidUp
{
get { return _bidUp; }
set
{
_bidUp = value;
OnPropertyChanged("BidUp");
}
}
public double Bid
{
get { return _bid; }
set
{
BidUp = (value > this.Bid);
_bid = value;
OnPropertyChanged("Bid");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
And my xaml code for the DataGridCell:
<DataTemplate>
<Border x:Name="BidBoder" Background="AliceBlue" BorderThickness="0">
<TextBlock Text="{Binding Bid}" x:Name="BidTextBlock" HorizontalAlignment="Right"></TextBlock>
</Border>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding BidUp}" Value="True">
<DataTrigger.EnterActions>
<StopStoryboard BeginStoryboardName="bidUpStoryboard"/>
<StopStoryboard BeginStoryboardName="bidDownStoryboard"/>
<BeginStoryboard Name="bidUpStoryboard">
<Storyboard BeginTime="00:00:00">
<ColorAnimation
BeginTime="00:00:00"
Duration="0:0:0.1"
To="LimeGreen"
AutoReverse="True"
Storyboard.TargetName="BidBoder"
Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)">
</ColorAnimation>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
<DataTrigger Binding="{Binding BidUp}" Value="False">
<DataTrigger.EnterActions>
<StopStoryboard BeginStoryboardName="bidUpStoryboard"/>
<StopStoryboard BeginStoryboardName="bidDownStoryboard"/>
<BeginStoryboard Name="bidDownStoryboard">
<Storyboard BeginTime="00:00:00">
<ColorAnimation
BeginTime="00:00:00"
Duration="0:0:0.1"
To="Red"
AutoReverse="True"
Storyboard.TargetName="BidBoder"
Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)">
</ColorAnimation>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</DataTemplate.Triggers>
Now what happens is if price goes up followed by a up, BidUp = true--->true
the propertyChanged does not trigger. Is there a way to trigeer a change even the _bidUp = value
does not change?
And I wanna ask generally if Property A = somevalue
is called, why OnPropertyChanged
is not called if the same value applies?
Upvotes: 1
Views: 320
Reputation: 27615
You have two ways of triggering the update of more than one property in the same setter:
public double Bid
{
get { return _bid; }
set
{
BidUp = (value > this.Bid);
_bid = value;
OnPropertyChanged("Bid");
OnPropertyChanged("BidUp"); // notify both properties in the same setter
}
}
or yet
public double Bid
{
get { return _bid; }
set
{
BidUp = (value > this.Bid);
_bid = value;
OnPropertyChanged(null); // notify change of EVERY property - good if few properties in ViewModel
}
}
Upvotes: 2
Reputation: 11570
Consider binding to a property that represents the actual value and then use a multibinding (i.e. currentValue and previousValue) to update your UI.
Upvotes: 1