首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将单个控件绑定到视图模型属性

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

Stack Overflow用户
提问于 2018-06-02 08:51:55
回答 1查看 160关注 0票数 0

我正在尝试绑定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正在被正确更新,因为我可以在调试器中查看它的值。但是,我不能让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]);
    }
}

}
EN

回答 1

Stack Overflow用户

发布于 2018-06-02 09:16:22

它不起作用,因为您当前绑定到字符串值"ProgressViewModel";相反,您应该绑定到一个实例到一个ProgressViewModel类型的对象。尝试:

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

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

下面是如何定义ns的。如果ProgressViewModel是在命名空间WpfApp1中定义的,那么ns应该写成这样:xmlns:ns="clr-namespace:WpfApp1"

namespace WpfApp1
{
    public class ProgressViewModel
    {
    //...
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50652580

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档