BRDroid
BRDroid

Reputation: 4408

XAML TextBox isReadOnly Binding

I am trying to make a textbox read only using Binding in Windows 8.1 apps. I have tried some code from the internet which does not work. Can you suggest any simplest way to do it, I am very new to the concept Binding.

XAML

<TextBox x:Name="tbOne"  IsReadOnly="{Binding Path=setread, Mode=OneWay}" />
<Button Content="isReadonlyBinding" x:Name="isReadonlyBinding" Click="isReadonlyBinding_Click"></Button>

XAML.CS

public static readonly DependencyProperty IsReadOnlyProperty = DependencyProperty.Register(
    "setread",
    typeof(bool),
    typeof(MainPage),
    new PropertyMetadata(false)
    );

public bool setread
{
    get { return (bool)GetValue(IsReadOnlyProperty); }
    set { SetValue(IsReadOnlyProperty, value); }

}

private void isReadonlyBinding_Click(object sender, RoutedEventArgs e)
{
    setread = true;
}

Upvotes: 4

Views: 7857

Answers (2)

dytori
dytori

Reputation: 487

Implement INotifyPropertyChanged on your code behind. Then modify the property as follows:

private bool _setread;
public bool Setread
{
    get { return _setread; }
    set { 
      if(_seatread == value) return; 
      _setread = value;
      RaisePropertyChanged("Setread"); 
    }
}

Give a name to root element like x:Name="root", and bind to Setread with ElementName=page. Note that it is much better to prepare a view model. A view-model-code-behind is just a quick workaround.

Upvotes: 0

alex10
alex10

Reputation: 85

try this.

<page X:name="PageName">
IsReadOnly="{Binding ElementName=PageName,Path=setread, Mode=OneWay}"

Upvotes: 3

Related Questions