绑定 - 控件未更新



我已将控件绑定到"描述"进度的类类型的某些属性。

一切似乎都很好:

  • NotifyPropertyChanged 似乎有效:当属性更改时它会引发(在更新 ProgressViewModel 类实例的属性的情况下(
  • 在代码运行时查看 XAML 代码时,在绑定属性下会显示正确的值(例如:进度等于 0.1(。

问题是视觉上没有任何变化。因此:控件不会更新。都。

XAML:

    <DockPanel LastChildFill="False">
        <DockPanel LastChildFill="False">
            <StackPanel x:Name="ProgressPart"
                        DockPanel.Dock="Top"
                        Visibility="{Binding Visible, Converter={StaticResource ValueConverterBoolToVisibility}}">
                <ProgressBar  Value="{Binding Path=Progress, Mode=OneWay, Converter={StaticResource ValueConverterTest}}" />
                <StackPanel Orientation="Horizontal">
                    <TextBlock>
                    <Run Text="Action:" />
                    <Run Text="{Binding Path=ActionName, Mode=OneWay}" />
                    </TextBlock>
                </StackPanel>
                <TextBlock Text="{Binding Path=ActionProgress, Mode=OneWay }" />
            </StackPanel>

            <StackPanel x:Name="MasterOKPart"
                        DockPanel.Dock="Top">
            </StackPanel>
            <StackPanel x:Name="MasterNotOKPart"
                        DockPanel.Dock="Top">
            </StackPanel>
            <StackPanel x:Name="FooterPart"
                        DockPanel.Dock="Bottom">
            </StackPanel>
        </DockPanel>
    </DockPanel>
</UserControl>

控制代码隐藏:

public partial class ContentAnomalies : UserControl, INotifyPropertyChanged, IViewControl
{
    private ProgressViewModel _ProgressViewModel;
    public ProgressViewModel ProgressViewModel
    {
        get
        {
            return _ProgressViewModel;
        }
        set
        {
            _ProgressViewModel = value;
            NotifyPropertyChanged();
        }
    }
    private bool _Prepared;
    public bool Prepared
    {
        get
        {
            return _Prepared;
        }
        set
        {
            if (value == false)
            {
                _Prepared = value;
                return;
            }
            if (_Prepared==false)
            {
                ProgressViewModel = new ProgressViewModel
                {
                    Visible = true,
                    Token = new CancellationTokenSource()
                };
                Thread myNewThread = new Thread(() => Auditor.AuditContentMD(Globals.ThisAddIn.Application.ActivePresentation, this, ProgressViewModel));
                myNewThread.Start();
                //ProgressViewModel.Visible = false;
            }
        }
    }
    public ContentAnomalies()
    {
        InitializeComponent();
        //ProgressViewModel = new ProgressViewModel();
        //ProgressViewModel.Visible = true;
        this.ProgressPart.DataContext = ProgressViewModel;
    }
    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
    public void Prepare()
    {
        throw new NotImplementedException();
    }
}

正在修改进度的方法视图模型:

内部静态空隙审计内容MD(PPT.演示文稿前置、内容异常控制、进度视图模型进度视图模型( { 列表内容审核 = 返回内容审核((;

    progressViewModel.Visible = true;
    progressViewModel.Progress = 0;
    progressViewModel.ActionName = "Loading presentation objects...";
    List<Anomaly> anomalies = new List<Anomaly>();
    Audit audit;
    AuditsSupport.SpacesPositions = null;
    DateTime timeStart = DateTime.Now;
    AuditsSupport.Shapes = SupportVSTO.ReturnAllShapesFromPresentation(Globals.ThisAddIn.Application.ActivePresentation, progressViewModel, 0, (double)1 / (contentAudits.Count + 1));
    TimeSpan timeShapes = DateTime.Now - timeStart;
    for (int i = 0; i < contentAudits.Count; i++)
    {
        DateTime timeStartInternal = DateTime.Now;
        if (progressViewModel.Token.IsCancellationRequested)
        {
            return;
        }
        else
        {
            audit = contentAudits[i];
            progressViewModel.ActionName = audit.Name;
            anomalies.AddRange(contentAudits[i].PerformAudit(pres, (double)(i + 1) / (contentAudits.Count + 1), (double)1 / (contentAudits.Count + 1), progressViewModel));
        }
        progressViewModel.Progress = (double)(i + 2) / (contentAudits.Count + 1);
    }
    TimeSpan timeWhole = DateTime.Now - timeStart;
    MessageBox.Show("all:" + "    " + timeWhole.TotalSeconds);
    //control.ShowProgressBar(false);
    //control.AddAuditsListControlToPanel(anomalies, pres);
}

和PogressViewModelClass:

public class ProgressViewModel: INotifyPropertyChanged
{
    private double _Progress;
    public double Progress
    {
        get
        {
            return _Progress;
        }
        set
        {
            _Progress = value;
            NotifyPropertyChanged();
        }
    }
    private string _ActionName;
    public string ActionName
    {
        get
        {
            return _ActionName;
        }
        set
        {
            _ActionName = value;
            NotifyPropertyChanged();
        }
    }
    private string _ActionProgress;
    public string ActionProgress
    {
        get
        {
            return _ActionProgress;
        }
        set
        {
            _ActionProgress = value;
            NotifyPropertyChanged();
        }
    }
    private bool _Visible;
    public bool Visible
    {
        get
        {
            return _Visible;
        }
        set
        {
            _Visible = true;
            NotifyPropertyChanged();
        }
    }
    private CancellationTokenSource _Token;
    public CancellationTokenSource Token
    {
        get
        {
            return _Token;
        }
        set
        {
            _Token = value;
            NotifyPropertyChanged();
        }
    }
    public ProgressViewModel()
    {
    }
    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

你知道怎么解决吗?

您是否尝试过将 PropertyName 添加到 NotifyPropertyChanged?如"通知属性已更改("操作名称"(">

可能它在你的绑定中,它应该是这样的:

TextBlock Text="{Binding ActionProgress, UpdateSourceTrigger=PropertyChanged}">

不需要模式=单向,因为它是默认设置

最新更新