绑定 - 控件未更新

时间:2018-04-16 14:38:58

标签: c# wpf data-binding

我已将控件绑定到“描述”进度的类类型的属性。

一切似乎都没问题:

  • NotifyPropertyChanged似乎有效:当属性发生变化时会引发(如果更新ProgressViewModel类实例的属性)
  • 在查看xaml代码时,在代码运行时,在绑定属性下显示正确的值(例如:Progress等于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();
    }
}

正在修改progressViewModel的方法:

internal static void AuditContentMD(PPT.Presentation pres,ContentAnomalies control,ProgressViewModel progressViewModel)     {         列出contentAudits = ReturnContentAudits();

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

}

你知道如何解决它吗?

1 个答案:

答案 0 :(得分:-1)

您是否尝试将PropertyName添加到NotifyPropertyChanged? 比如'NotifyPropertyChanged(“ActionName”)'

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

TextBlock Text =“{Binding ActionProgress,UpdateSourceTrigger = PropertyChanged}”

不需要Mode = OneWay作为默认设置