报告在Task中执行的ViewModel方法的进度

时间:2017-08-25 07:42:58

标签: c# wpf mvvm progress

我正在尝试报告在任务中执行的ViewModel(MVVM Light)方法的进度。 ProgressViewModel包含ProgressModel属性,该属性提供描述当前状态的属性。这些属性绑定到Xceed BusyIndicator。到现在为止还挺好。但它没有按预期工作。 ProgressModel.IsRunning绑定到BusyIndiciator.IsBusy并切换可见性 - 这有效。 ProgressModel.DescriptionProgressModel.Percentage绑定到TextBlock.Text / ProgressBar.Value - 但它们未更新...

所以......自IsRunning起作用以来,DescriptionProgress的错误是什么......?

// Model
public sealed class ProgressModel: ObservableObject
{
    private string m_description;
    private float m_percentage;
    private bool m_running;

    public string Description
    {
        get { return m_description; }
        set { Set(ref m_description, value); }
    }
    public float Percentage
    {
        get { return m_percentage; }
        set { Set(ref m_percentage, value); }
    }
    public bool IsRunning
    {
        get { return m_running; }
        set{Set(ref m_running, value);}
    }

    public void Initiate()
    {
        Description = string.Empty;
        Percentage = 0;
        IsRunning = true;
    }
}
// Command
private void DownloadWatch()
{
    DispatcherHelper.UIDispatcher.Invoke(() =>
    {
        Progress.Initiate();
    });
    using (var watch = new PolarWatch())
    {
        watch.Connect();
        for (var i = 0; i < watch.Sessions.Count; i++)
        {
            DispatcherHelper.UIDispatcher.Invoke(() =>
            {
                Progress.Description = $"Writing session data for '{session.DateTime}'...";
            });
        }
    }
    DispatcherHelper.UIDispatcher.Invoke(() =>
    {
        Progress.IsRunning = false;
    });
}
// View
<xctk:BusyIndicator Name="Busy" IsBusy="{Binding Progress.IsRunning}">
    <xctk:BusyIndicator.BusyContentTemplate>
        <DataTemplate>
            <Grid Width="150">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding Progress.Description}"/>
                <ProgressBar Grid.Row="1" Grid.Column="0" Value="{Binding Progress.Percentage}" Height="14" Margin="0,5,0,0"/>
            </Grid>
        </DataTemplate>
    </xctk:BusyIndicator.BusyContentTemplate>
    <xctk:BusyIndicator.ProgressBarStyle>
        <Style TargetType="ProgressBar">
            <Setter Property="Visibility" Value="Collapsed"/>
        </Style>
    </xctk:BusyIndicator.ProgressBarStyle>

1 个答案:

答案 0 :(得分:1)

DataContext中根元素的ItemTemplate与控件本身的DataContext不同。试试这个:

<TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding DataContext.Progress.Description, RelativeSource={RelativeSource AncestorType=xctk:BusyIndicator}}"/>
<ProgressBar Grid.Row="1" Grid.Column="0" Value="{Binding DataContext.Progress.Percentage,RelativeSource={RelativeSource AncestorType=xctk:BusyIndicator}}" Height="14" Margin="0,5,0,0"/>

或设置DataContext的{​​{1}}:

Grid