WPF how to define TextBox with conditional IsReadOnly

Dmtr_Grms 331 Reputation points
2023-03-07T17:27:26.8066667+00:00

I have a WPF with a TabItem that has a TextBox that contain the Last Name (Binded to Last Name SQL Server DB field). In the same record I have a flag that define if the person is an Associate or not.

I would like to define a conditional IsReadOnly on the TextBox. If Associate is True then the TextBox should be ReadOnly. How is it possible to manage simply that necessity?

Code behind is C#.

Thanks in advance for your suggestions.

Developer technologies Windows Presentation Foundation
Developer technologies XAML
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.5K Reputation points
    2023-03-07T17:48:33.73+00:00

    If you already have a binding for last name, then add a similar binding:

    <TextBox Text="{Binding LastName}"  IsReadOnly="{Binding IsAssociate}" . . . /> 
    

    where IsAssociate is a bool property. Extend your source data object to include such property.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.