【发布时间】:2021-09-07 07:13:21
【问题描述】:
使用 Prism 库的 WPF MVVM 模式中的事件绑定不适用于子用户控件中的按钮。
我有一个基本视图PreparationMockView,其中有一个内容控件,它是一个子视图LeftBarPrepMockView。现在单击LeftBarPrepMockView 中的按钮时,不会触发SelectedTaskCommand。
下面是父视图和子视图以及各自的视图模型。
-
父视图:
<UserControl x:Class="PreparationMockUp.Views.PreparationMockView" 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:mvvm="http://prismlibrary.com/" xmlns:preparationMockUp="clr-namespace:PreparationMockUp" mvvm:ViewModelLocator.AutoWireViewModel="True" mc:Ignorable="d"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="200" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <ContentControl Grid.Row="0" Grid.Column="0" mvvm:RegionManager.RegionName="{x:Static preparationMockUp:PreparationMockUpModule.LeftBarPreparationRegionName}" /> <TabControl Grid.Row="0" Grid.Column="1" TabStripPlacement ="Left" Background="#f2f2f3"> <Grid> <UserControl> -
子视图:
<UserControl x:Class="PreparationMockUp.Views.LeftBarPrepMockView" 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:mvvm="http://prismlibrary.com/" mvvm:ViewModelLocator.AutoWireViewModel="True" mc:Ignorable="d"> <Grid Background="#1a2231"> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <StackPanel Grid.Row="1"> <!-- Bind Rows using the default StackPanel for the ItemsPanel --> <ItemsControl Name="taskDisplayList" ItemsSource="{Binding TaskList, Mode=TwoWay}"> <!-- Set the Template for each row to a TextBlock and another ItemsControl --> <ItemsControl.ItemTemplate> <DataTemplate> <Grid Margin="5"> <Grid.ColumnDefinitions> <ColumnDefinition Width="20" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Button Name="SelectTaskButton" Grid.Column="1" Command="{Binding SelectedTaskCommand}" CommandParameter="{Binding}" IsEnabled="{Binding IsEnabled, Mode= TwoWay}" BorderThickness ="0" HorizontalAlignment="Left" Style="{StaticResource TaskButton}" Content="{Binding Name}"> </Button> </Grid> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> </StackPanel> </Grid> <UserControl> -
视图模型:
namespace PreparationMockUp.ViewModels { public class LeftBarPrepMockViewModel : ViewModelBase { private readonly IEventAggregator _eventAggregator; private Tasks tasks; readonly string path = ConfigurationManager.AppSettings["ConfigPath"].ToString(); public LeftBarPrepMockViewModel(IEventAggregator ea) { _eventAggregator = ea; _eventAggregator.GetEvent<PubSubEvent<PubSubMessages>>().Subscribe((message) => InsertedCylinder(), ThreadOption.UIThread, true, x => x.Equals(PubSubMessages.InsertedBuildCylinder)); SelectedTaskCommand = new DelegateCommand(NavigateToSelectedTask); } public ICommand SelectedTaskCommand { get; set; } private List<Task> _taskList; public List<Task> TaskList { get { return _taskList; } set { _taskList = value; OnPropertyChanged(new PropertyChangedEventArgs("TaskList")); } } private void NavigateToSelectedTask(){ } } }
【问题讨论】:
标签: c# wpf xaml data-binding prism