XAML TextBox 是只读绑定
XAML TextBox isReadOnly Binding
我正在尝试使用 Windows 8.1 应用程序中的绑定将文本框设置为只读。我已经尝试了一些来自互联网的代码,但它们不起作用。
你能推荐任何最简单的方法吗,我对绑定这个概念还很陌生。
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");
}
}
为根元素命名,例如 x:Name="root"
,并使用 ElementName=page
绑定到 Setread
。请注意,准备一个视图模型会更好。视图模型代码隐藏只是一种快速解决方法。
我正在尝试使用 Windows 8.1 应用程序中的绑定将文本框设置为只读。我已经尝试了一些来自互联网的代码,但它们不起作用。 你能推荐任何最简单的方法吗,我对绑定这个概念还很陌生。
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");
}
}
为根元素命名,例如 x:Name="root"
,并使用 ElementName=page
绑定到 Setread
。请注意,准备一个视图模型会更好。视图模型代码隐藏只是一种快速解决方法。