WPF - MVVM - 用户控件绑定 [英] WPF - MVVM - UserControl binding

查看:62
本文介绍了WPF - MVVM - 用户控件绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个简单的 UserControl 示例,在 TextBox 中显示当前日期时间,每秒更新四次.

I'm trying to realize a simple example of a UserControl, showing in a TextBox the current DateTime, updated four times each second.

我创建了一个简单的用户控件:

I create a simple user control:

<UserControl x:Class="UC.TestUC"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:UC"
             mc:Ignorable="d" 
             d:DesignHeight="50" d:DesignWidth="100">
    <d:UserControl.DataContext>
        <local:TestUC_VM/>
    </d:UserControl.DataContext>
    <Grid Background="Azure">
        <TextBox Text="{Binding TestString}"/>
    </Grid>
</UserControl>

它的 ViewModel 在哪里:

Where its ViewModel is:

namespace UC
{
    public class TestUC_VM : INotifyPropertyChanged
    {
        private string _testString;
        public string TestString
        {
            get => _testString;
            set
            {
                if (value == _testString) return;
                _testString = value;
                OnPropertyChanged();
            }
        }

        public TestUC_VM()
        {
            TestString = "Test string.";
        }

        public event PropertyChangedEventHandler PropertyChanged;
        void OnPropertyChanged([CallerMemberName] string propertyName = null) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

主窗口 XAML:

<Window x:Class="UC.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:UC"
        mc:Ignorable="d"
        Title="MainWindow" Height="100" Width="200">
    <Window.DataContext>
        <local:MainWindow_VM/>
    </Window.DataContext>
    <Window.Resources>
        <local:TestUC_VM x:Key="TestUC_VM"/>
    </Window.Resources>
    <Grid>
        <local:TestUC DataContext="{StaticResource TestUC_VM}"/>
    </Grid>
</Window>

及其视图模型:

namespace UC
{
    public class MainWindow_VM
    {
        public TestUC_VM _uc_VM;

        public MainWindow_VM()
        {
            _uc_VM = new TestUC_VM();
            Task.Run(() => ChangeString());
        }

        public async Task ChangeString()
        {
            while (true)
            {
                _uc_VM.TestString = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
                await Task.Delay(250);
            }
        }
    }
}

即使我使用调试器看到我正在通过 TestString setter,MainWindow 也没有更新.我很确定我在 MainWindow 中设置 UC 的 DataContext 时遗漏了一些微不足道的东西,但经过几个小时的浏览和思考后,我还是没能找到什么.

Even though I see with debugger that I'm passing through the TestString setter, the MainWindow is not updated. I'm quite sure I'm missing something trivial in setting DataContext of UC in MainWindow, but I've not been able to find what after several hours of browsing and thinking.

感谢任何帮助.

推荐答案

表达式

<local:TestUC DataContext="{StaticResource TestUC_VM}"/>

将 TestUC_VM 资源的值分配给 UserControl 的 DataContext.这是与主视图模型的 _uc_VM 成员不同的对象,您稍后将更新该成员.

assigns the value of the TestUC_VM resource to the UserControl's DataContext. This is a different object than the _uc_VM member of the main view model, which you are later updating.

把成员变成公共财产

public TestUC_VM UcVm { get; } = new TestUC_VM();

<local:TestUC DataContext="{Binding UcVm}"/>

像这样更新视图模型:

UcVm.TestString = ...

这篇关于WPF - MVVM - 用户控件绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆