将单个控件绑定到视图模型属性

时间:2018-06-02 00:51:55

标签: c# .net xaml

我正在尝试绑定TextBlock控件的文本,如下所示:

<TextBlock DataContext="ProgressViewModel" Text="{Binding FileName}" HorizontalAlignment="Center"/>

“ProgressViewModel”是我的项目中存在的一个类,它公开了FileName属性:

    private string _fileName;
    public string FileName
    {
        get { return _fileName; }
        set { this.MutateVerbose(ref _fileName, value, RaisePropertyChanged()); }
    }

我知道FileName正在正确更新,因为我可以在debuggger中查看它的值。但是,我无法让XAML绑定工作。我试图避免在Window.Resources中设置主绑定,因为有些文章已经建议,因为我在这个项目中有多个视图模型。

全视图模型:

using MaterialDesignThemes.Wpf;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading.Tasks;

namespace MaterialDesignTest

{
    public class ProgressViewModel : INotifyPropertyChanged
    {

    System.Windows.Forms.Timer progressTimer;

    private double _saveProgressButton;
    public double SaveProgressButton
    {
        get { return _saveProgressButton; }
        set { this.MutateVerbose(ref _saveProgressButton, value, RaisePropertyChanged()); }
    }
    private string _fileName;
    public string FileName
    {
        get { return _fileName; }
        set { this.MutateVerbose(ref _fileName, value, RaisePropertyChanged()); }
    }

    private bool _isSaveComplete;
    public bool IsSaveComplete
    {
        get { return _isSaveComplete; }
        private set { this.MutateVerbose(ref _isSaveComplete, value, RaisePropertyChanged()); }
    }

    private bool _isSaving;
    public bool IsSaving
    {
        get { return _isSaving; }
        private set { this.MutateVerbose(ref _isSaving, value, RaisePropertyChanged()); }
    }

    int progress = 0;
    int cycles = 0;
    public ProgressViewModel()
    {

    }
    public void KickOffProgressTimer()
    {
        progressTimer = new System.Windows.Forms.Timer();
        progressTimer.Tick += new EventHandler(progressTimerTick);
        progressTimer.Interval = 40;
        progressTimer.Start();
    }

    private async void progressTimerTick(object sender, EventArgs e)
    {
        FileName = SelectRandomString();

        if (progress < 100 && cycles < 2)
        {
            if (progress == 99)
            {
                cycles++;
                progress = 0;
            }

            IsSaveComplete = false;
            IsSaving = true;
            progress++;
            SaveProgressButton = progress;
        }
        else
        {
            IsSaveComplete = true;
            IsSaving = false;
            progressTimer.Enabled = false;
            SaveProgressButton = 0;

            await NonBlockingDelay(1750);

            DialogHost.CloseDialogCommand.Execute(null, null);
        }
    }
    async Task NonBlockingDelay(int value)
    {
        await Task.Delay(value);
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private Action<PropertyChangedEventArgs> RaisePropertyChanged()
    {
        return args => PropertyChanged?.Invoke(this, args);
    }

    static string SelectRandomString()
    {
        var random = new Random();
        var questions = new List<string>{
            @"C:\Files\Filename1",
            @"C:\Filename2",
            @"C:\Filename3",
            @"C:\Filename4",
            @"C:\Temp\Files\Filename5",
            @"C:\Filename6",
            @"C:\Demo\LongFolderName\Filename7",
            @"C:\Filename8",
            @"C:\Filename9",
        };
        int index = random.Next(questions.Count);
        return(questions[index]);
    }
}

}

1 个答案:

答案 0 :(得分:1)

它无效,因为您目前正在绑定字符串值"ProgressViewModel";相反,您应该将实例绑定到类型为ProgressViewModel的对象。尝试:

<TextBlock Text="{Binding FileName}" HorizontalAlignment="Center">
    <TextBlock.DataContext>
        <ns:ProgressViewModel />
    </TextBlock.DataContext>
</TextBlock>

在这里,我假设您在根标记中将包含ProgressViewModel的命名空间定义为XML命名空间ns。我还假设ProgressViewModel有一个公共无参数构造函数。

以下是您定义ns的方法。如果ProgressViewModel在命名空间WpfApp1中定义,则ns应如下所示:xmlns:ns="clr-namespace:WpfApp1"

namespace WpfApp1
{
    public class ProgressViewModel
    {
    //...
    }
}