WPF用命令绑定按钮但无法触发
WPF bind Button with Command but cannot trigger
我目前正在学习 MVVM,但遇到了这个问题。
所以基本上我有一个主视图,比如
<Window x:Class="ManufacturingToolV2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:views="clr-namespace:ManufacturingToolV2.View"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid DataContext="{StaticResource viewModel}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<views:BurnInView />
</Grid>
而“BurnInView”是一个带有按钮的单独视图:
<UserControl x:Class="ManufacturingToolV2.View.BurnInView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:viewmodel="clr-namespace:ManufacturingToolV2.ViewModel"
xmlns:converter="clr-namespace:ManufacturingToolV2.Converters"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
>
<UserControl.Resources>
<viewmodel:BurnInTestViewModel x:Key="viewModel"/>
<converter:BoolToStringConverter x:Key="boolToStringConverter" />
</UserControl.Resources>
<Grid DataContext="{StaticResource viewModel}" Background="White">
<Button Content="Start Test" HorizontalAlignment="Stretch" Margin="10,86,10,0" VerticalAlignment="Top" Command="{Binding StartTestCommand, Mode=OneWay}">
<Button.Style>
<Style TargetType="Button">
<Style.Triggers>
<DataTrigger Binding="{Binding TestRunning}" Value="True">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
<DataTrigger Binding="{Binding TestRunning}" Value="False">
<Setter Property="IsEnabled" Value="True"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>>
</Grid>
我的“BurnInView”绑定到一个视图模型,这样:
public class BurnInTestViewModel : ViewModelBase
{
public ICommand StartTestCommand;
public BurnInTestViewModel()
{
StartTestCommand = new DelegateCommand((time) => { StartBurnIn(3000); }, canExecute => { return !TestRunning; });
}
void StartBurnIn(int time)
{
cycles = 0;
timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(time);
timer.Tick += (seder, e) => { cycles++; };
timer.Start();
}
}
但是现在当我点击按钮时,没有任何反应,它甚至没有点击函数的开头...
谁能告诉我这是怎么回事?
您只是忘记将您的命令声明为 属性:
public ICommand StartTestCommand { get; set; }
要跟踪此类错误,我建议您在调试期间使用选项卡“XAML 绑定失败”。它可以帮助快速理解为什么 Binding 不起作用,在您的示例中:
我目前正在学习 MVVM,但遇到了这个问题。 所以基本上我有一个主视图,比如
<Window x:Class="ManufacturingToolV2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:views="clr-namespace:ManufacturingToolV2.View"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid DataContext="{StaticResource viewModel}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<views:BurnInView />
</Grid>
而“BurnInView”是一个带有按钮的单独视图:
<UserControl x:Class="ManufacturingToolV2.View.BurnInView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:viewmodel="clr-namespace:ManufacturingToolV2.ViewModel"
xmlns:converter="clr-namespace:ManufacturingToolV2.Converters"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
>
<UserControl.Resources>
<viewmodel:BurnInTestViewModel x:Key="viewModel"/>
<converter:BoolToStringConverter x:Key="boolToStringConverter" />
</UserControl.Resources>
<Grid DataContext="{StaticResource viewModel}" Background="White">
<Button Content="Start Test" HorizontalAlignment="Stretch" Margin="10,86,10,0" VerticalAlignment="Top" Command="{Binding StartTestCommand, Mode=OneWay}">
<Button.Style>
<Style TargetType="Button">
<Style.Triggers>
<DataTrigger Binding="{Binding TestRunning}" Value="True">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
<DataTrigger Binding="{Binding TestRunning}" Value="False">
<Setter Property="IsEnabled" Value="True"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>>
</Grid>
我的“BurnInView”绑定到一个视图模型,这样:
public class BurnInTestViewModel : ViewModelBase
{
public ICommand StartTestCommand;
public BurnInTestViewModel()
{
StartTestCommand = new DelegateCommand((time) => { StartBurnIn(3000); }, canExecute => { return !TestRunning; });
}
void StartBurnIn(int time)
{
cycles = 0;
timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(time);
timer.Tick += (seder, e) => { cycles++; };
timer.Start();
}
}
但是现在当我点击按钮时,没有任何反应,它甚至没有点击函数的开头...
谁能告诉我这是怎么回事?
您只是忘记将您的命令声明为 属性:
public ICommand StartTestCommand { get; set; }
要跟踪此类错误,我建议您在调试期间使用选项卡“XAML 绑定失败”。它可以帮助快速理解为什么 Binding 不起作用,在您的示例中: