使用INotifyPropertyChanged的结合(Binding using INotifyP

2019-09-28 14:37发布

在项目中,我想改变每当一个新的买入价是在一个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不会触发。 有没有一种方法来trigeer改变甚至_bidUp = value不会改变?
我想一般问,如果Property A = somevalue被调用时,为什么OnPropertyChanged如果相同的值适用不叫?

Answer 1:

你有触发在同一个二传手多个属性的更新的方法有两种:

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
    }
}


Answer 2:

考虑结合代表实际值的属性,然后使用multibinding(即CurrentValue的和PREVIOUSVALUE)来更新UI。



文章来源: Binding using INotifyPropertyChanged