表达式交互数据触发器最初未正确显示绑定值

Expression Interactivity Data Trigger Does Not Correctly Display Bound Value Originally

我有一个表达式交互 DataTrigger,它根据绑定 TimeSpan 属性 更改 TextBlockText 属性。当该值大于或等于 Timespan.Zero 时,文本将是 属性 的值。当值小于零时,值变为“??:??:??”。

相关代码如下:

<i:Interaction.Triggers>
    <ei:DataTrigger Binding="{Binding InspectionService.TimeRemainingForPart}" Comparison="GreaterThanOrEqual" Value="{x:Static sys:TimeSpan.Zero}">
        <ei:ChangePropertyAction PropertyName="Text" Value="{Binding InspectionService.TimeRemainingForPart}" />
    </ei:DataTrigger>

    <ei:DataTrigger Binding="{Binding InspectionService.TimeRemainingForPart}" Comparison="LessThan" Value="{x:Static sys:TimeSpan.Zero}">
        <ei:ChangePropertyAction PropertyName="Text" Value="??:??:??" />
    </ei:DataTrigger>
</i:Interaction.Triggers>

TimeRemainingForPart 属性 通过 InspectionService 中的定时器更新。当计时器为 运行 时,一切正常。当计时器停止时(这会将 TimeRemainingForPart 设置为 Timespan.Zero),视图会按预期显示“00:00:00”。但是,当应用程序首次加载时,文本块中没有显示任何内容。我什至尝试过从 InspectionService 的构造函数更改 value/notifying 和 属性,但没有任何反应。

我总是可以使用一个名为 "TimespanLessThanZeroConverter" 或类似的转换器的标准 WPF DataTrigger,但是关于为什么我当前的操作方式在应用程序启动时不起作用的任何想法 -起来了吗?

编辑:忘记提及我曾尝试在我的服务构造函数中调用 TimeRemainingForPart 属性 上的 OnPropertyChanged 以防万一某些事情没有得到正确通知,但这似乎没有完成任何事情。

Edit2:为 Textblock 以及 ViewModel 和服务的相关部分添加完整 XAML。

XAML:

<TextBlock Grid.Row="1" FontSize="56" FontWeight="Bold" Text="{Binding InspectionService.TimeRemainingForPart}">
    <TextBlock.Style>
        <Style TargetType="TextBlock" BasedOn="{StaticResource StatusIndicatorTextBlockStyle}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding InspectionService.InspectionExecutionState}" Value="{x:Static enum:InspectionExecutionStates.Paused}">
                    <DataTrigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard Duration="0:0:1" AutoReverse="True" RepeatBehavior="Forever">
                                <ColorAnimation Storyboard.TargetProperty="Foreground.(SolidColorBrush.Color)" To="Transparent" />
                            </Storyboard>
                        </BeginStoryboard>
                    </DataTrigger.EnterActions>

                    <DataTrigger.ExitActions>
                        <BeginStoryboard>
                            <Storyboard Duration="0:0:1">
                                <ColorAnimation Storyboard.TargetProperty="Foreground.(SolidColorBrush.Color)" To="White" />
                            </Storyboard>
                        </BeginStoryboard>
                    </DataTrigger.ExitActions>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>

    <i:Interaction.Triggers>
        <ei:DataTrigger Binding="{Binding InspectionService.TimeRemainingForPart}" Comparison="GreaterThanOrEqual" Value="{x:Static sys:TimeSpan.Zero}">
            <ei:ChangePropertyAction PropertyName="Text" Value="{Binding InspectionService.TimeRemainingForPart}" />
        </ei:DataTrigger>

        <ei:DataTrigger Binding="{Binding InspectionService.TimeRemainingForPart}" Comparison="LessThan" Value="{x:Static sys:TimeSpan.Zero}">
            <ei:ChangePropertyAction PropertyName="Text" Value="??:??:??" />
        </ei:DataTrigger>
    </i:Interaction.Triggers>
</TextBlock>

视图模型:

public class MyViewModel : IMyViewModel
{
    public IInspectionService InspectionService { get; private set; }

    public MyViewModel (IInspectionService inspectionService)
    {
        this.InspectionService = inspectionService;
    }
}

服务:

public class InspectionService : BindableBase, IInspectionService
{
    private readonly IModeService _modeService;
    private readonly IRemainingTimeFileServiceFactory _remainingTimeFileServiceFactory;

    private string _inspectionCell;

    private readonly DispatcherTimer _timeRemainingForPartTimer;

    #region IInspectionService Members

    private InspectionExecutionStates _inspectionExecutionState;
    public InspectionExecutionStates InspectionExecutionState
    {
        get { return this._inspectionExecutionState; }
        private set { this.SetProperty(ref this._inspectionExecutionState, value); }

    private string _inspectionName;
    public string InspectionName
    {
        get { return this._inspectionName; }
        private set { this.SetProperty(ref this._inspectionName, value); }
    }

    private TimeSpan _timeRemainingForPart;
    public TimeSpan TimeRemainingForPart
    {
        get { return this._timeRemainingForPart; }
        private set { this.SetProperty(ref this._timeRemainingForPart, value); }

    private TimeSpan _totalTimeRemaining;
    public TimeSpan TotalTimeRemaining
    {
        get { return this._totalTimeRemaining; }
        private set { this.SetProperty(ref this._totalTimeRemaining, value); }
    }

    #endregion

    public InspectionService(IModeService modeService, IRemainingTimeFileServiceFactory remainingTimeFileServiceFactory)
    {
        this._modeService = modeService;
        this._remainingTimeFileServiceFactory = remainingTimeFileServiceFactory;

        this._timeRemainingForPartTimer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(1) };
        this._timeRemainingForPartTimer.Tick += this.TimeRemainingForPartTimerOnTick;
    }

    private void StartSelectedInspection(InspectionPlanInfo inspectionPlanInfo)
    {
        this.SetInspectionProperties(inspectionPlanInfo);
        this.StartInspection
    }

    #endregion

    #region Private Methods

    private void StartInspection()
    {
        this.TotalTimeRemaining = this._remainingTimeFileServiceFactory.GetIRemainingTimeFileService(this._inspectionCell).GetInspectionTime(this.InspectionName);
        this.TimeRemainingForPart = this._modeService.IsStudyActive ? TimeSpan.MinValue : this.TotalTimeRemaining;
        this._timeRemainingForPartTimer.Start();
    }

    private void StopInspection()
    {
        this.ClearInspectionProperties();
        this._timeRemainingForPartTimer.Stop();
    }

    private void SetInspectionProperties(InspectionPlanInfo inspectionPlanInfo)
    {
        this.InspectionName = inspectionPlanInfo.InspectionName;
        this._inspectionCell = inspectionPlanInfo.Cell;
    }

    private void ClearInspectionProperties()
    {
        this.InspectionName = "";
        this.TimeRemainingForPart = TimeSpan.Zero;
        this.TotalTimeRemaining = TimeSpan.Zero;
    }

    private void TimeRemainingForPartTimerOnTick(object sender, EventArgs eventArgs)
    {
        if (this.TimeRemainingForPart < TimeSpan.Zero)
        {
            this._timeRemainingForPartTimer.Stop();
        }
        else
        {
            this.TimeRemainingForPart -= TimeSpan.FromSeconds(1);
            this.TotalTimeRemaining -= TimeSpan.FromSeconds(1);
        }
    }
}

编辑 3:

显然没有转换器是做不到的,所以TextBlock代码修改如下:

<TextBlock Grid.Row="1" FontSize="56" FontWeight="Bold">
    <TextBlock.Style>
        <Style TargetType="TextBlock" BasedOn="{StaticResource StatusIndicatorTextBlockStyle}">
            <Setter Property="Text" Value="{Binding InspectionService.TimeRemainingForPart}" />

            <Style.Triggers>
                <DataTrigger Binding="{Binding InspectionService.TimeRemainingForPart, Converter={StaticResource TimespanLessThanZeroConverter}}"
                        Value="True">
                    <Setter Property="Text" Value="??:??:??" />
                </DataTrigger>

                <DataTrigger Binding="{Binding InspectionService.InspectionExecutionState}" Value="{x:Static enum:InspectionExecutionStates.Paused}">
                    <DataTrigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard Duration="0:0:1" AutoReverse="True" RepeatBehavior="Forever">
                                <ColorAnimation Storyboard.TargetProperty="Foreground.(SolidColorBrush.Color)" To="Transparent" />
                            </Storyboard>
                        </BeginStoryboard>
                    </DataTrigger.EnterActions>

                    <DataTrigger.ExitActions>
                        <BeginStoryboard>
                            <Storyboard Duration="0:0:1">
                                <ColorAnimation Storyboard.TargetProperty="Foreground.(SolidColorBrush.Color)" To="White" />
                            </Storyboard>
                        </BeginStoryboard>
                    </DataTrigger.ExitActions>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>

为了 completeness/posterity 转换器看起来像这样:

using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace CurrentInspection.Converters
{
    public class TimespanLessThanZeroConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (!(value is TimeSpan))
            {
                return false;
            }

            return (TimeSpan)value < TimeSpan.Zero;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return DependencyProperty.UnsetValue;
        }
    }
}

您是否像这样将 TextBoxText 属性 绑定到源 属性?:

<TextBlock Text="{Binding InspectionService.TimeRemainingForPart}">
    <i:Interaction.Triggers>
        <ei:DataTrigger Binding="{Binding InspectionService.TimeRemainingForPart}" Comparison="LessThan" Value="{x:Static sys:TimeSpan.Zero}">
            <ei:ChangePropertyAction PropertyName="Text" Value="??:??:??" />
        </ei:DataTrigger>
    </i:Interaction.Triggers>
</TextBlock>

那么只需在视图模型的构造函数中将 TimeRemainingForPart 属性 设置为默认值即可。

如您所见,我删除了第一个交互触发器。您最好将文本 属性 绑定到源 属性 并使用转换器而不是使用两个 ChangePropertyAction。或者绑定到视图模型的字符串 属性 returns 基于 TimeSpan 值的正确字符串。