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;
}
try this.
<page X:name="PageName">
IsReadOnly="{Binding ElementName=PageName,Path=setread, Mode=OneWay}"
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.