我在我的視圖Home.xaml中有以下按鈕。我將它綁定到名爲StartStopLabel的屬性。我在相同的視圖中實現了接口ICommand
,點擊Start後我可以將標籤更改爲文本「Stop」(這是我在視圖的構造函數中設置的初始狀態,如this.StartStopLabel="Start",this.ButtonStatus="click on start button")
,但我不是能夠做相反的操作,即將按鈕的標籤從「停止」更改爲「開始」。我的意思是說ICommand
當按鈕標籤顯示「停止」時不會收到點擊事件通知。如何在xaml中使用ICommand更改按鈕的標籤?
一旦用戶點擊「停止」按鈕(即當按鈕標籤顯示文本「停止」時)我想要將文本塊「BtnSTatus」的文本更改爲「您已經點擊了開始按鈕」並返回到「點擊開始按鈕」當按鈕標籤再次顯示文本「開始」時
有什麼建議如何解決這兩個問題?
筆者認爲:
<Button Name="btnStartStop" Content="{Binding StartStopLabel}" Command="{Binding ClickCommand}" />
<TextBlock Name="BtnStatus" Content="{Binding ButtonStatus}">
View.Cs代碼:
private string _startStopLabel;
public string StartStopLabel
{
get
{
return _startStopLabel;
}
set
{
_startStopLabel = value;
RaisePropertyChanged("StartStopLabel");
}
}
private string _ButtonStatus;
public string ButtonStatus
{
get
{
return _ButtonStatus;
}
set
{
_ButtonStatus = value;
RaisePropertyChanged("ButtonStatus");
}
}
點擊指令事件,這是在View.cs的ICommand實現的一部分:我建議
public System.Windows.Input.ICommand ClickCommand
{
get
{
return new DelegateCommand((o) =>
{
this.StartStopLabel = "Stop";
Task.Factory.StartNew(() =>
{
//call service on a background thread here...
});
});
}
}
你設置了DataContext = this?綁定區分大小寫。 ButtonStatus(在你的RaisePropertyChanged中)與ButtonSTatus(在你的XAML中)不是一回事。它看起來像只在點擊時將StartStopLabel文本設置爲停止。你永遠不會將其重置爲開始。 –
是的,datacontext已正確設置並使用ButtonStatus更新了錯字。 – krrishna