简体   繁体   English

XAML TextBox isReadOnly Binding

[英]XAML TextBox isReadOnly Binding

I am trying to make a textbox read only using Binding in Windows 8.1 apps. 我试图使用Windows 8.1应用程序中的Binding只读取文本框。 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. 你能提出任何最简单的方法吗,我对Binding这个概念很新。

XAML XAML

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

XAML.CS 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. 在你的代码背后实现INotifyPropertyChanged 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 . 为root元素命名,如x:Name="root" ,并使用ElementName=page绑定到Setread Note that it is much better to prepare a view model. 请注意,准备视图模型要好得多。 A view-model-code-behind is just a quick workaround. 视图模型代码隐藏只是一种快速的解决方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM