我在MainWindow中有一些UserControl。我试图通过引发UserControl事件并在MainWindow中订阅该事件来在视图之间切换。
我已经看到类似问题的答案,但似乎没有一个对我有帮助。
我尝试了下面的代码。运行代码时,placeOrder
中的MainWindowViewModel
方法被命中,在其中引发PlaceOrderRequested
事件。每当引发此事件时,都应调用navigateToOrder
中的MainWindowViewModel
方法,但不会发生。 MainWindowViewModel
中的其他命令被绑定到某些按钮。请帮助我找到问题。
public class CustomerViewModel : ViewModel
{
public Command<Customer> PlaceOrderCommand { get; } //uses placeOrder function
public event Action<Customer> PlaceOrderRequested = delegate {};
private void placeOrder(Customer cust)
{//hitting here when put a breakpoint
PlaceOrderRequested(cust);
}
}
public class MainWindowViewModel: ViewModel
{
public Command<string> NavCommand { get; }
private OrderViewModel orderViewModel = new OrderViewModel();
private CustomerViewModel customerViewModel = new CustomerViewModel();
public MainWindowViewModel()
{
customerViewModel.PlaceOrderRequested += navigateToOrder;
}
private void navigateToOrder(Customer cust)
{//not hitting here when put a breakpoint
orderViewModel.Customer = cust;
CurrentViewModel = orderViewModel;
}
}
public class ViewModel : INotifyPropertyChanged
{
protected void OnPropertyChanged(string propName)
{
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
public event PropertyChangedEventHandler PropertyChanged = delegate {};
}
这是我的观点,我删除了不必要的行以使代码最少:
MainWindow
<Window x:Class="MVVM_FirstAPP.MainWindow"
xmlns:local="clr-namespace:MVVM_FirstAPP"
xmlns:vm="clr-namespace:MVVM_FirstAPP.ViewModels"
xmlns:views="clr-namespace:MVVM_FirstAPP.Views"
>
<Window.Resources>
<DataTemplate DataType="{x:Type vm:CustomerViewModel}">
<views:CustomerView></views:CustomerView>
</DataTemplate>
<DataTemplate DataType="{x:Type vm:OrderPrepViewModel}">
<views:OrderPrepView></views:OrderPrepView>
</DataTemplate>
<DataTemplate DataType="{x:Type vm:OrderViewModel}">
<views:OrderView></views:OrderView>
</DataTemplate>
</Window.Resources>
<Window.DataContext>
<local:MainWindowViewModel />
</Window.DataContext>
<Grid>
<Button Content="Customers"
HorizontalAlignment="Left"
Grid.Row="0"
Command="{Binding NavCommand}"
CommandParameter="Customer"/>
<Button Content="OrderPrep"
Grid.Row="0"
Command="{Binding NavCommand}"
CommandParameter="OrderPrep"/>
<ContentControl Content="{Binding CurrentViewModel}"
Grid.Row="1" />
</Grid>
</Window>
客户视图
<UserControl x:Class="MVVM_FirstAPP.Views.CustomerView"
xmlns:local="clr-namespace:MVVM_FirstAPP.Views"
xmlns:customerVM="clr-namespace:MVVM_FirstAPP.ViewModels">
<UserControl.DataContext>
<customerVM:CustomerViewModel />
</UserControl.DataContext>
<Grid>
<DataGrid ItemsSource="{Binding Customers}"
AutoGenerateColumns="False"
SelectedItem="{Binding SelectedCustomer}"
Grid.Row="0"
CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTextColumn Header="ID"
Binding="{Binding Id}"
Width="Auto" />
<DataGridTextColumn Header="Name"
Binding="{Binding FullName, Mode=OneWay}"
Width="*" />
<DataGridTemplateColumn Width="Auto">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<!--On Click of this button I want to switch views. This button is calling PlaceOrder command which in turn is raising PlaceOrderRequested event-->
<Button Content="Place Order"
Command="{Binding DataContext.PlaceOrderCommand, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
CommandParameter="{Binding}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</UserControl>
我处于“客户视图”中,当单击“下订单”按钮时,我正在举起placeOrder
事件的地方遇到PlaceOrderRequested
。 MainWindowViewModel
订阅了此活动,但控制权没有得到控制。