WPF组件内容在查看模型更改后未更新

发布于 2025-02-01 23:17:54 字数 1356 浏览 2 评论 0原文

我有以下视图模型:

public class ViewModel : ObservableObject
{
    public string Watermark { get; set; } = "Loading...";
}

其中observableObject来自Microsoft.toolkit.mvvm.com.ponentmodel它实现inotifyPropertyChangedinotifyPropertyPropertyChanging

我有以下XAML:

<xctk:WatermarkTextBox>
    <xctk:WatermarkTextBox.WatermarkTemplate>
        <DataTemplate>
            <ContentControl Content="{Binding DataContext.Watermark, RelativeSource={RelativeSource AncestorType=Window}}"></ContentControl>
        </DataTemplate>
    </xctk:WatermarkTextBox.WatermarkTemplate>
</xctk:WatermarkTextBox>

最后,以下C#代码:

private async void Window_Loaded(object sender, RoutedEventArgs e)
{
    Dispatcher.Invoke(() =>
    {
        // Assigning property of textbox directly with .Watermark = "" does not work

        // Updating view model property doesn't work either
        Model.Watermark = "Done";
    });
}

当调用窗口加载方法时,Watermark Textbox的水印不会更新以反映其假定的新值,“ DONED”

可能缺乏一般的WPF理解,但是如果有人可以给出一个指针,以说明为什么会失败,我将非常感谢它。

谢谢!

I have the following view model:

public class ViewModel : ObservableObject
{
    public string Watermark { get; set; } = "Loading...";
}

where ObservableObject is from Microsoft.Toolkit.Mvvm.ComponentModel which implements INotifyPropertyChanged and INotifyPropertyChanging.

I have the following XAML:

<xctk:WatermarkTextBox>
    <xctk:WatermarkTextBox.WatermarkTemplate>
        <DataTemplate>
            <ContentControl Content="{Binding DataContext.Watermark, RelativeSource={RelativeSource AncestorType=Window}}"></ContentControl>
        </DataTemplate>
    </xctk:WatermarkTextBox.WatermarkTemplate>
</xctk:WatermarkTextBox>

And lastly the following C# code:

private async void Window_Loaded(object sender, RoutedEventArgs e)
{
    Dispatcher.Invoke(() =>
    {
        // Assigning property of textbox directly with .Watermark = "" does not work

        // Updating view model property doesn't work either
        Model.Watermark = "Done";
    });
}

When the window loaded method gets called however, the watermark of the watermark textbox does not update to reflect its supposed new value, "Done".

It might be a lack of general WPF understanding but if anyone could give a pointer as to why this might fail I would greatly appreciate it.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妳是的陽光 2025-02-08 23:17:54

您无法正确使用observableObject。这不是魔术(与某些使用Fody之类的框架的解决方案不同),因此您仍然必须自己完成工作以传播变化。请注意

public class User : ObservableObject
{
    private string name;

    public string Name
    {
        get => name;
        set => SetProperty(ref name, value);
    }
}

因此,您必须遵循这种模式:

public class ViewModel : ObservableObject
{
    private string watermark;

    public ViewModel() 
    {
        Watermark = "Loading...";
    }

    public string Watermark
    {
        get => watermark;
        set => SetProperty(ref watermark, value);
    }
}

You aren't using ObservableObject properly. It's not magic (unlike some solutions that use frameworks such as Fody), so you still have to do the work yourself to propagate changes. Note the example in the documentation.

public class User : ObservableObject
{
    private string name;

    public string Name
    {
        get => name;
        set => SetProperty(ref name, value);
    }
}

So you'll have to follow that pattern:

public class ViewModel : ObservableObject
{
    private string watermark;

    public ViewModel() 
    {
        Watermark = "Loading...";
    }

    public string Watermark
    {
        get => watermark;
        set => SetProperty(ref watermark, value);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文