0

I am using Caliburn.Micro in a WPF project with c#.

I wanted a way, like in android to find a "TextBlock" in a xaml view by it's "id" so that I can manipulate its properties.

I was thinking of doing something like this but for c#:

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

so I can collapse and make it visible again.

<TextBlock x:Name="MyTextBlockId"
               Text="Incorrect user credentials. Forgot password, click here" 
               Visibility="Collapsed"/>
Daniel
  • 2,028
  • 20
  • 18
  • From which class are you trying to manipulate the TextBlock? – Eric Oct 26 '17 at 17:35
  • It is a login page. So on a failed attempt I was going to un collapse a TextBlock "that would say forgot password click here". I was going to do this inside the ViewModel – Daniel Oct 26 '17 at 17:36
  • 1
    You shouldn't be performing control manipulations inside the viewmodel. The view should be aware of the viewmodel. Not the other way around. If, for example, you had a boolean property named IsBadLogin, you could bind the visibility of the TextBlock to that property and use a BooleanToVisibilityConverter. – Eric Oct 26 '17 at 17:43

1 Answers1

1

MVVM Approach

ViewModel

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}"/>
Eric
  • 1,737
  • 1
  • 13
  • 17