2013-01-25 65 views
0

我目前正在使用MahApps地鐵庫對我的舊版wpf應用程序的外觀進行擺弄。我與卡住控制:ToggleSwitch我可以綁定幾乎所有的東西,但命令。 當我嘗試綁定如下命令,Metro風格的Windows 7 WPF應用程序-toggleSwitch-

<Controls:ToggleSwitch Header="Start Playing" OnLabel="Stop" OffLabel="Play" 
    IsChecked="{Binding ToggleRecordCommand}" 
    CommandParameter="{Binding}" /> 

我得到一個錯誤等;

Error 62 A TwoWay or OneWayToSource binding cannot work on the read-only property 'ToggleRecordCommand' of type 'RecorderApp.View.MainWindowViewModel'. 

而且它告訴我沒有CommandParameter。我如何將動作綁定到這個?

+0

爲了使用CommandParameter您必須指定命令whick我認爲將在被點擊的事件引發不布爾器isChecked財產 – iltzortz

+0

通常情況下,我可以綁定命令按鈕。但是切換開關不可能嗎?我曾計劃在視圖模型 – mechanicum

回答

1

首先,正如Brendan所說,IsChecked屬性必須綁定一個具有INotifyPropertyChanged的通用屬性,而不是ICommand類型。

爲了與Command綁定,最簡單的解決方法是使用Click(或Checked)事件與xaml.cs代碼隱藏工作。

在XAML中,如下所示。

<ToggleButton x:Name="recordButton" 
Checked="OnRecordButton_Checked" 
IsChecked={Binding IsRecording} /> 

在代碼隱藏中,如下所示。

private void OnRecordButton_Checked(object sender, RoutedEventArgs e) 
{ 
    if (recordButton.IsChecked.GetValueOrDefault()) 
    { 
     // Do your own logic to execute command. with-or-without command parameter. 
     viewModel.ToggleRecordCommand.Execute(null); 
    } 
} 

而且,在ViewModel(假設)中,如下所示。

// Property for toggle button GUI update 
public bool IsRecording{ 
get{ return _isRecording;} 
set{ 
    _isRecording = value; 
    NotifyPropertyChanged("IsRecording"); 
    } 
} 

public ICommand ToggleRecordCommand{ 
// Your command logic. 
} 
+0

處理所有操作很好的解決方法:)謝謝 – mechanicum

0

IsCheckedbool?屬性,如果將ICommand傳遞給它,可能無法正常工作。 Source code

如果您希望看到這種支持,請提出project site的問題,我們可以進一步討論。

相關問題