3 回答

TA貢獻1804條經驗 獲得超2個贊
我解決了它如下
<Label Text="{Binding TimerValue, TargetNullValue='00:00', FallbackValue='00:00', StringFormat='{0:mm\\:ss}'}"
BindingContext="{Binding Source={x:Reference MyPage}, Path=BindingContext}">
綁定錯誤的原因,BindingContext必須是BindableObject。 BindingContext是可綁定對象,它又引用ViewModel對象,并且Label.Text必須是BindableProperty可綁定對象。當我引用它時Text={Binding ViewModel.TimerValue,它試圖在中找到可綁定屬性,Mypage但是ViewModel它只是一個公共屬性,而不是 Bindable 對象BindingContext = ViewModel將其轉換為 Bindable 對象,因此我不得不對 Source 使用這種方式,而 Text 只是調用該引用的 bindingcontext 的路徑
感謝所有的建議!非常感謝這個社區的及時響應!

TA貢獻1801條經驗 獲得超8個贊
遲到的答案,但可能會對某人有所幫助。如果您在后面的代碼中編寫布局。您可以以這種方式將上下文綁定到源視圖模型上下文。我將一個按鈕綁定到視圖模型中的命令。
btn.SetBinding(Button.CommandProperty, new Binding("BindingContext.YourCommand", source: YourListView));
btn.SetBinding(Button.CommandParameterProperty, ".");

TA貢獻1808條經驗 獲得超4個贊
兩步..
為您的父視圖提供一個引用名稱
x:Name="viewName"
(綁定到 ViewModel 的視圖)綁定如下:
"{Binding BindingContext.MenuTapCommand, Source={x:Reference viewName}}"
這行得通。
- 3 回答
- 0 關注
- 192 瀏覽
添加回答
舉報