簡體   English   中英

XAML TextBox isReadOnly Binding

[英]XAML TextBox isReadOnly Binding

我試圖使用Windows 8.1應用程序中的Binding只讀取文本框。 我嘗試過一些無法正常工作的代碼。 你能提出任何最簡單的方法嗎,我對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;
}

試試這個。

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

在你的代碼背后實現INotifyPropertyChanged 然后按如下方式修改屬性:

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

為root元素命名,如x:Name="root" ,並使用ElementName=page綁定到Setread 請注意,准備視圖模型要好得多。 視圖模型代碼隱藏只是一種快速的解決方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM