由于某種原因,我無法激活我的Button.
在我的應用程式中,我有一個TextBox, 填充了用戶從對話框中選擇的檔案路徑。我也有一個ComboBox讓用戶選擇一種語言。
我想要做的是激活我的按鈕Start,當FilePath不為空并且用戶選擇了一種語言時。
[AddINotifyPropertyChangedInterface]
public class AudioPageViewModel {
public string? FilePath { get; set; }
public bool IsWorking { get; set; }
public bool CanPress { get; set; }
public string? SelectedItem { get; set; }
public List<string>? Languages { get; set; }
public Visibility CanShow { get; set; }
public DialogHelper Dialog { get; }
public Command PickFileCommad { get; set; }
public Command StartCommand { get; set; }
public AudioPageViewModel() {
InitListLanguages();
Dialog = new DialogHelper();
CanShow = Visibility.Hidden;
PickFileCommad = new Command(PickFileAction);
StartCommand = new Command(StartAction, CanStartAction);
}
private bool CanStartAction(object arg) {
if (string.IsNullOrEmpty(SelectedItem) ||
string.IsNullOrEmpty(FilePath)) {
return false;
}
return true;
}
private void StartAction(object obj) {
throw new NotImplementedException();
}
private void PickFileAction() {
var filePath = Dialog.GetFilePath(ConstantsHelpers.AUDIO);
FilePath = filePath;
}
private void InitListLanguages() {
Languages = new List<string>() {
"English",
"Spanish"
};
<Grid Margin="20">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Button
VerticalAlignment="Top"
Command="{Binding PickFileCommad}"
Content="Pick a file"
FontWeight="Bold" />
<Label
Grid.Row="1"
Margin="0,5,0,0"
VerticalContentAlignment="Top"
Content="language of the file" />
<TextBox
Grid.Column="1"
Margin="10,0,10,0"
VerticalAlignment="Top"
IsReadOnly="True"
Text="{Binding FilePath}" />
<ComboBox
Grid.Row="1"
Grid.Column="1"
Margin="10,0,10,0"
SelectedItem="{Binding SelectedItem}"
HorizontalAlignment="Stretch"
ItemsSource="{Binding Languages}">
</ComboBox>
<Label
Grid.Row="3"
Grid.ColumnSpan="2"
Margin="10,0,10,0"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
Content="This will not take long"
Visibility="{Binding CanShow}" />
<ui:ProgressRing
Grid.Row="2"
Grid.ColumnSpan="2"
Width="60"
Height="60"
Margin="10,0,10,0"
IsActive="{Binding IsWorking}" />
<Button
Grid.Row="4"
Grid.ColumnSpan="2"
Margin="10,0,10,10"
HorizontalAlignment="Stretch"
VerticalAlignment="Bottom"
Content="Start"
Command="{Binding StartCommand}"
IsEnabled="{Binding CanPress}" />
</Grid>
uj5u.com熱心網友回復:
一個關鍵問題是您同時使用 aCommand和IsEnabled系結。您應該只使用一個,因為它們會相互干擾。命令系結將根據其CanExceute方法的結果設定按鈕的啟用狀態。系結IsEnabled也將覆寫它。我建議僅使用命令并將邏輯CanPress從CanStartAction.StartCommand
使用命令
命令表明它們是否可以通過其CanExecute方法結果執行。但是,有不同的命令實作會以不同的方式觸發此方法的重新評估。
使用命令管理器中繼命令
如果您使用使用RequerySuggested事件的命令實作,例如:
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested = value; }
remove { CommandManager.RequerySuggested -= value; }
}
然后洗掉IsEnabled系結就足夠了,因為它是由用戶界面中的輸入觸發的。
使用顯式方法的中繼命令
另一個變體是命令實作,其中公開了一個方法以顯式地重新評估其狀態,例如RaiseCanExcuteChanged. 在這種情況下,CanExecuteChanged事件被顯式引發,并且系結命令的 UI 元素被觸發CanExecute再次執行并更新其自己的啟用狀態。
不幸的是,Fody PropertyChanged庫不支持會觸發重新評估可以執行的命令的屬性。已經有一個請求,但被拒絕了,他們不會執行它。
- 為命令擴展 DependsOnAttribute #281
一種解決方法是創建On<Property>Changed方法,Fody 會在相應的更改時<Property>自動呼叫這些方法,請參閱Wiki 中的 On_PropertyName_Changed。然后這些方法呼叫顯式重新評估方法。
public void OnSelectedItemChanged()
{
StartCommand.RaiseCanExecuteChanged();
Debug.WriteLine("SelectedItem Changed");
}
public void OnFilePathChanged()
{
StartCommand.RaiseCanExecuteChanged();
Debug.WriteLine("FilePath Changed");
}
使用 IsEnabled
僅使用時IsEnabled,您必須將CanStartAction要執行的邏輯移動到CanPress. 在這種情況下,這將是一個不能使用 Fody 的顯式實作。
- 自定義設定器 #157
此外,您不能使用命令,因此Click處理程式將是一個丑陋的選擇。這就是為什么我強烈建議您改用命令。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/520479.html
標籤:C#wpfxml数据绑定fody-propertychanged
