我有這個奇怪的問題,系結似乎完全被忽略了。
我的 xml
<Button IsEnabled="{Binding ButtonEnabled}" x:Name="ButtonOK" BackgroundColor="Green" TextColor="White" Text="OK"/>
我的C#
private bool _buttonEnabled = false;
public bool ButtonEnabled
{
get
{
// breakpoint 1, which never hits with value = false
return _buttonEnabled;
}
set
{
// breakpoint 2, which hits
_buttonEnabled = value;
OnPropertyChanged(nameof(ButtonEnabled));
}
}
private void ChassisEntry_TextChanged(object sender, TextChangedEventArgs e)
{
ButtonEnabled = ChassisEntry.Text != "";
}
private void PageScan_Appearing(object sender, EventArgs e)
{
ChassisEntry.Text = "";
}
我希望當此頁面打開時會ButtonOK被禁用,但事實并非如此。
當我設定斷點時breakpoint 1(在 getter 中)永遠不會命中,就像IsEnabled="{Binding ButtonEnabled}"忽略 xaml 一樣。確實命中,
與breakpoint 2value = false
我在這里想念什么?
我用谷歌搜索了這個問題并發現了許多類似的問題,但是給出的所有解決方案都對我的問題沒有幫助。
Button IsEnabled 系結無法正常作業
如何在所有條目都填滿之前禁用按鈕?
禁用/啟用保存按鈕基于必填欄位為空或使用行為
等為空
uj5u.com熱心網友回復:
我猜你正在使用xaml.cs頁面來保存你的系結,因此如果你這樣做,有兩種方法可以做到這一點
在 InitializeComponent 之前或之后將 BindingContext 設定為建構式中的當前類
BindingContext= this;
或在您的 XAML 中
<ContentPage
....
x:Name="currentPage">
在你的按鈕中
<Button IsEnabled="{Binding ButtonEnabled, Source={x:Reference currentPage}}"
uj5u.com熱心網友回復:
我會這樣做:在禁用的 XAML 中設定我的按鈕。
<Button IsEnabled="False" x:Name="ButtonOK" BackgroundColor="Green" TextColor="White" Text="OK"/>
然后在我的 Entry 控制元件上添加屬性 TextChanged。
<Entry x:Name="ChassisEntry"
PlaceholderColor="DarkGray"
TextChanged="ChassisEntryChanged">
在 xaml.cs 檔案中:
private void ChassisEntryChanged(object sender, TextChangedEventArgs e)
{
if (e.NewTextValue.Text != "")
{
ButtonOK.IsEnabled = true;
}
else
{
ButtonOK.IsEnabled = false;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/424213.html
標籤:xml xamarin xamarin.forms 捆绑
上一篇:將ax:Static與MultiBinding一起使用
下一篇:無法設定選取器默認值
