如何从 View 中获取对象 TextBlock 并在 ViewModel 中对其进行操作?

How to get object TextBlock from View and manipulate it in the ViewModel?

我在 C# 的 WPF 项目中使用 Caliburn.Micro。

我想要一种方法,比如在 android 中,通过它的 "id" 在 xaml 视图中找到 "TextBlock",以便我可以操纵它的属性。

我正在考虑做这样的事情,但对于 c#:

TextBlock textblock = (TextBlock ) myView.findViewById(R.id.myTextBlock);

所以我可以折叠并使其再次可见。

<TextBlock x:Name="MyTextBlockId"
               Text="Incorrect user credentials. Forgot password, click here" 
               Visibility="Collapsed"/>

MVVM 方法

视图模型

class MyViewModel : PropertyChangedBase
{
    private bool _isBadLogin;

    public bool IsBadLogin
    {
        get => _isBadLogin;
        set => Set(ref _isBadLogin, value);
    }
}

XAML

<TextBlock x:Name="MyTextBlockId"
           Text="Incorrect user credentials. Forgot password, click here" 
           Visibility="{Binding IsBadLogin, Converter={StaticResource BooleanToVisibilityConverter}"/>