在我的项目中,我想在新的出价进入时更改Datagrid单元格的背景。 我用
设置它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));
}
}
我的DataGridCell的xaml代码:
<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>
现在发生的事情是,如果价格上涨后跟上涨,BidUp = true--->true
propertyChanged不会触发。即使_bidUp = value
没有改变,有没有办法改变变化?我想问一般是否调用Property A = somevalue
,为什么OnPropertyChanged
如果相同的值不被调用适用?
答案 0 :(得分:2)
您有两种方法可以在同一个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
}
}
或
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
}
}
答案 1 :(得分:1)
考虑绑定到表示实际值的属性,然后使用多绑定(即currentValue和previousValue)来更新UI。