4

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;
}
jhmt
  • 1,401
  • 1
  • 11
  • 15
BRDroid
  • 3,920
  • 8
  • 65
  • 143
  • Dont you need to use `RaisePropertyChanged` on `setread`? I am not quite sure how it goes on Windows App. – dytori Oct 19 '15 at 00:51

2 Answers2

3

try this.

<page X:name="PageName">
IsReadOnly="{Binding ElementName=PageName,Path=setread, Mode=OneWay}"
alex10
  • 85
  • 8
  • HI alex, that is amazing. it works fine now. could you tell me why we need to specify the element name – BRDroid Oct 19 '15 at 01:11
  • My understanding is you have defined "setread" as a property of your page control, If you don't specify the element, binding will go to find "setread" in its DataContext. – alex10 Oct 19 '15 at 01:18
0

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.

dytori
  • 487
  • 4
  • 12
  • Edited not to use `DependencyProperty` as it seemed not mandatory. – dytori Oct 19 '15 at 01:14
  • can you suggest how to use INotifyPropertyChanged. I am completely new to this. thanks – BRDroid Oct 19 '15 at 01:15
  • I expect a copy and paste will work. http://stackoverflow.com/questions/1315621/implementing-inotifypropertychanged-does-a-better-way-exist – dytori Oct 19 '15 at 01:28
  • And, sorry, my answer is not cool. As alex10 suggests, you need to set source of binding (see edit). `ElementName=PageName` may be just enough. Still, however, it is better that you become familiar with notification system. – dytori Oct 19 '15 at 01:31
  • Let me note that `DependencyProperty` has innately a notification system. – dytori Oct 19 '15 at 01:34
  • 1
    Both way will go, and alex's is simpler for sure. Plus, `DependencyProperty` is visible through XAML, and you can control the value like ``, even if you dont like it. Preparing a view model is a bit laborious on the other hand, but gives you more flexible ways to design your control. – dytori Oct 19 '15 at 01:43