ListView 绑定到集合,如何访问父级中的事件class?
ListView binds to a collection, how to access an event in a parent class?
在我的 WinRT 项目中,我有一个 ListView,如下所示;
<ListView Grid.Row="1"
ItemsSource="{Binding Path=Survey.SelectedSection.QuestionsAndNavigation, Mode=TwoWay}"
IsSwipeEnabled="False"
SelectionMode="None"
ScrollViewer.VerticalScrollBarVisibility="Auto"
Background="White"
ItemTemplateSelector="{StaticResource ResourceKey=QuestionDisplay}"
ItemContainerStyle=
"{StaticResource ResourceKey=QuestionListViewItemContainerStyle}">
</ListView>
注意这行;
ItemsSource="{绑定路径=Survey.SelectedSection.QuestionsAndNavigation}"
集合 QuestionsAndNavigation 以 UserControl 中的一些导航按钮结尾;
<StackPanel Orientation="Horizontal" >
<Button Margin="0,40,40,0"
IsEnabled="{Binding Path=PreviousSectionId, Converter={StaticResource ResourceKey=IntBooleanConverter}}"
Command="{Binding Path=NavigateToPreviousSectionCommand}"><< Save and Previous Section</Button>
<Button Margin="0,40,0,0"
IsEnabled="{Binding Path=NextSectionId, Converter={StaticResource ResourceKey=IntBooleanConverter}}"
Command="{Binding Path=NavigateToNextSectionCommand}">Save and Next Section >></Button>
</StackPanel>
并显示如下;
现在的问题是在绑定路径中:Survey.SelectedSection.QuestionsAndNavigation 我希望命令位于伟大的 GrandParent class SurveyPageViewModel(包含 Survey 对象)而不是嵌入式集合中class 问题和导航。默认情况下,数据上下文查找错误 class。
重要的是要认识到这不是继承。 "Survey" 此处映射到 SurveyPageViewModel class,其中包含映射到 SurveyViewModel class 的 Survey、映射到 SectionViewModel class 的 SelectedSection 以及包含 QuestionsAndNavigation 的 SelectedSection QuestionViewModel 集合 classes.
所以 QuestionViewModel 中的代码:
private void NavigateToNextSection()
{
NavigateToSection(this.NextSectionId);
}
private void NavigateToPreviousSection()
{
NavigateToSection(this.PreviousSectionId);
}
private void NavigateToSection(int sectionId)
{
NavService.Navigate("NewSection", sectionId);
}
不转到 SurveyPageViewModel,我无法在 QuestionViewModel class 中设置 NavService,因为它不存在,它是 SurveyPageViewModel 中的 属性。
编辑。
所以感谢 Eric 的回答,我的 ListView 包含一个 x:Name = "ListResponses"。我在我的按钮路径中使用它:
<StackPanel Orientation="Horizontal" >
<Button Margin="0,40,40,0"
IsEnabled="{Binding Path=PreviousSectionId,
Converter={StaticResource ResourceKey=IntBooleanConverter}}"
Command="{Binding
Path=DataContext.SaveThenPreviousSectionCommand,
ElementName=ListResponses}"><< Previous Section</Button>
<Button Margin="0,40,0,0"
IsEnabled="{Binding Path=NextSectionId,
Converter={StaticResource ResourceKey=IntBooleanConverter}}"
Command="{Binding
Path=DataContext.SaveThenNextSectionCommand,
ElementName=ListResponses}">Next Section >></Button>
</StackPanel>
我已经将事件连接放在 SurveyPageViewModel 中,但由于某种原因它们没有连接,我想知道答案;
public ICommand SaveThenPreviousSectionCommand { get; private set; }
public ICommand SaveThenNextSectionCommand { get; private set; }
...
(在构造函数中)
SaveThenPreviousSectionCommand = new DelegateCommand(DoSaveThenPreviousSection);
SaveThenNextSectionCommand = new DelegateCommand(DoSaveThenNextSection);
...
private async void DoSaveThenPreviousSection()
{
var i = 1;
}
private async void DoSaveThenNextSection()
{
var i = 0;
}
编辑:这是页面的完整 XAML 文件
<prism:VisualStateAwarePage x:Class="M.Survey.Views.SurveyPage"
x:Name="pageRoot"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:views="using:M.Survey.Views"
xmlns:local="using:M.Survey"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:prism="using:Microsoft.Practices.Prism.StoreApps"
xmlns:mvvm="using:Microsoft.Practices.Prism.Mvvm"
mvvm:ViewModelLocator.AutoWireViewModel="true"
xmlns:userControls="using:M.Survey.UserControls"
mc:Ignorable="d"
xmlns:converters="using:M.Survey.Converters"
Loaded="pageRoot_Loaded"
Unloaded="pageRoot_Unloaded"
xmlns:templateSelectors="using:M.Survey.TemplateSelectors">
<prism:VisualStateAwarePage.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Themes/AllQuestionDataTemplates.xaml" />
<ResourceDictionary Source="/Themes/Styles.xaml" />
<ResourceDictionary Source="/Themes/DataTemplates.xaml" />
</ResourceDictionary.MergedDictionaries>
<templateSelectors:QuestionTemplateSelector x:Key="QuestionDisplay"
TextEntry="{StaticResource TextEntry}"
DateTimeEntry="{StaticResource DateTimeEntry}"
BoolEntry="{StaticResource BoolEntry}"
IntEntry="{StaticResource IntEntry}"
SelectEntry="{StaticResource SelectEntry}"
DecimalEntry="{StaticResource DecimalEntry}"
LargeTextArea="{StaticResource LargeTextArea}"
SorEntry="{StaticResource SorEntry}"
Additional="{StaticResource AdditionalEntry}"
Miscellaneous="{StaticResource Miscellaneous}"
Dim="{StaticResource Dim}"
Signature="{StaticResource Signature}"
SectionNavigation="{StaticResource SectionNavigation}"/>
<converters:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
<converters:InverseBooleanConverter x:Key="InverseBooleanConverter" />
</ResourceDictionary>
</prism:VisualStateAwarePage.Resources>
<Grid>
<Grid.Background>
<SolidColorBrush Color="{StaticResource ResourceKey=AppBackgroundColor}" />
</Grid.Background>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="80" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid Grid.Row="0" Background="{StaticResource ResourceKey=MulalleyBlueBrush}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<StackPanel Orientation="Horizontal" Margin="20, 0, 0, 5">
<Button Margin="0, 0, 15, 0" Command="{Binding Path=TryGoHomeCommand}" Style="{StaticResource BackButtonStyle}" />
<TextBlock Style="{StaticResource ResourceKey=MediumHeaderTextBlockStyle}" Text="{Binding Path=Job.ShortDisplayAddress}" VerticalAlignment="Bottom" />
<StackPanel Margin="40, 0, 0, 6" Width="30" VerticalAlignment="Bottom" Height="30">
<ProgressRing Height="30" Width="30" Foreground="White" IsActive="{Binding Path=WorkInProgress}" Visibility="{Binding Path=WorkInProgress, Converter={StaticResource ResourceKey=BooleanToVisibilityConverter}}" />
</StackPanel>
<TextBlock Margin="10, 0, 0, 10" VerticalAlignment="Bottom" Text="{Binding Path=FeedbackMsg}" Style="{StaticResource ResourceKey=SmallHeaderTextBlockStyle}" />
</StackPanel>
<Grid Margin="0, 0, 15, 5" Grid.Column="1" VerticalAlignment="Bottom">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock VerticalAlignment="Bottom" Text="Internet" Style="{StaticResource ResourceKey=MediumHeaderTextBlockStyle}" />
<TextBlock Margin="10, 0, 15, 6" VerticalAlignment="Bottom" Grid.Column="1" Text="{Binding Path=ConnectionStatus}" Style="{StaticResource ResourceKey=SmallHeaderStyle}" />
<TextBlock VerticalAlignment="Bottom" Text="Server" Style="{StaticResource ResourceKey=MediumHeaderTextBlockStyle}" Grid.Column="2" />
<TextBlock Margin="10, 0, 15, 6" VerticalAlignment="Bottom" Grid.Column="3" Text="{Binding Path=VpnStatus}" Style="{StaticResource ResourceKey=SmallHeaderStyle}" />
<TextBlock VerticalAlignment="Bottom" Text="User" Style="{StaticResource ResourceKey=MediumHeaderTextBlockStyle}" Grid.Column="4" />
<TextBlock Margin="10, 0, 0, 6" VerticalAlignment="Bottom" Grid.Column="5" Text="{Binding Path=User.DisplayName}" Style="{StaticResource ResourceKey=SmallHeaderStyle}" />
</Grid>
<TextBlock Width="80" Margin="100, 0, 30, 0" Text="{Binding Path=AnswersCompleteDisplay}" Grid.Column="1" Style="{StaticResource ResourceKey=MediumHeaderTextBlockStyle}"/>
<Button Style="{StaticResource ResourceKey=SaveButtonStyle}" Content="Save" Grid.Column="2" Command="{Binding Path=SaveCommand}" />
</Grid>
</Grid>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="2*" />
</Grid.ColumnDefinitions>
<Grid>
<Grid.Background>
<SolidColorBrush Color="{StaticResource ResourceKey=MulalleyBlue}" Opacity=".2" />
</Grid.Background>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ListView
ItemsSource="{Binding Path=Survey.Sections, Mode=TwoWay}"
SelectedItem="{Binding Path=Survey.SelectedSection, Mode=TwoWay}"
IsSwipeEnabled="False"
SelectionMode="Single"
ItemContainerStyle="{StaticResource ResourceKey=ListViewItemContainerStyle}"
ItemTemplate="{StaticResource ResourceKey=SurveySectionDataTemplate}"
ScrollViewer.HorizontalScrollBarVisibility="Hidden"
ScrollViewer.VerticalScrollBarVisibility="Auto"
/>
</Grid>
<Grid Grid.Column="1" Margin="30, 30, 15, 20">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal">
<TextBlock Text="*" FontSize="40" FontWeight="Bold" Foreground="Red"/>
<TextBlock Text=" = Required " FontSize="20"/>
</StackPanel>
<ListView
x:Name="ListResponses"
Grid.Row="1"
ItemsSource="{Binding Path=Survey.SelectedSection.QuestionsAndNavigation, Mode=TwoWay}"
IsSwipeEnabled="False"
SelectionMode="None"
ScrollViewer.VerticalScrollBarVisibility="Auto"
Background="White"
ItemTemplateSelector="{StaticResource ResourceKey=QuestionDisplay}"
ItemContainerStyle=
"{StaticResource ResourceKey=QuestionListViewItemContainerStyle}">
</ListView>
</Grid>
</Grid>
</Grid>
</Grid>
<userControls:SurveyDialogUserControl />
<userControls:SurveyReassignedDialogUserControl />
<userControls:CopyFromUserControl />
</Grid>
<prism:VisualStateAwarePage.BottomAppBar>
<CommandBar Name="commandBar">
<CommandBar.SecondaryCommands>
<AppBarButton Label="Settings" Icon="Setting" Click="AppBarButton_Click" />
<AppBarButton Label="Log" Icon="Admin" Click="LogButton_Click" />
<AppBarButton Label="Refresh" Icon="Refresh" Command="{Binding Path=RefershAllDataCommand}" IsEnabled="{Binding Path=WorkInProgress, Converter={StaticResource ResourceKey=InverseBooleanConverter}}" Visibility="{Binding Path=VpnOnline, Converter={StaticResource ResourceKey=BooleanToVisibilityConverter}}" />
<AppBarButton Label="Copy From" Icon="Copy" Click="CopyButton_Click" Command="{Binding Path=ShowCopyFromCommand}" IsEnabled="{Binding Path=WorkInProgress, Converter={StaticResource ResourceKey=InverseBooleanConverter}}" />
</CommandBar.SecondaryCommands>
</CommandBar>
</prism:VisualStateAwarePage.BottomAppBar>
</prism:VisualStateAwarePage>
这是用户控件 XAML;
<UserControl
x:Class="M.Survey.UserControls.SectionNavigation"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:M.Survey.UserControls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:converters="using:M.Survey.Converters"
mc:Ignorable="d"
d:DesignHeight="30"
d:DesignWidth="400">
<UserControl.Resources>
<ResourceDictionary>
<converters:IntBooleanConverter x:Key="IntBooleanConverter" />
</ResourceDictionary>
</UserControl.Resources>
<Grid>
<StackPanel Orientation="Horizontal" >
<Button Margin="0,40,40,0"
IsEnabled="{Binding Path=PreviousSectionId,
Converter={StaticResource ResourceKey=IntBooleanConverter}}"
Command="{Binding
Path=DataContext.SaveThenPreviousSectionCommand,
ElementName=ListResponses}"><< Previous Section</Button>
<Button Margin="0,40,0,0"
IsEnabled="{Binding Path=NextSectionId,
Converter={StaticResource ResourceKey=IntBooleanConverter}}"
Command="{Binding
Path=DataContext.SaveThenNextSectionCommand,
ElementName=ListResponses}">Next Section >></Button>
</StackPanel>
</Grid>
</UserControl>
假设 ListView XAML 与 Button XAML 在同一个文件中(并且 ListView 是 x:Name="listView"):
<Button Margin="0,40,0,0"
IsEnabled="{Binding Path=NextSectionId, Converter={StaticResource ResourceKey=IntBooleanConverter}}"
Command="{Binding ElementName=listView, Path=DataContext.Survey.DESIRED_COMMAND}">Save and Next Section >></Button>
此外,您还可以在实例化时将调查视图模型的实例传递到问题视图模型中(取决于您创建视图模型的方式)。
最后,您还可以使用事件发布系统(例如 MVVM Light Messenger)在单击按钮时发布一个事件,调查已连线以响应。
我从 Eric 那里得到了很多帮助,Eric 给出了另一个答案。
为了在嵌入在它们自己的复杂设计中的 2 classes 之间进行通信,一种方法是使用 EventAggregator 模式。碰巧我正在开发的应用程序使用了 Prism,它是 EventAggregator 自己的实现。此模式使您能够发布一个有效载荷,该有效载荷由接收 class.
上的订阅事件拾取
在我的 WinRT 项目中,我有一个 ListView,如下所示;
<ListView Grid.Row="1"
ItemsSource="{Binding Path=Survey.SelectedSection.QuestionsAndNavigation, Mode=TwoWay}"
IsSwipeEnabled="False"
SelectionMode="None"
ScrollViewer.VerticalScrollBarVisibility="Auto"
Background="White"
ItemTemplateSelector="{StaticResource ResourceKey=QuestionDisplay}"
ItemContainerStyle=
"{StaticResource ResourceKey=QuestionListViewItemContainerStyle}">
</ListView>
注意这行; ItemsSource="{绑定路径=Survey.SelectedSection.QuestionsAndNavigation}" 集合 QuestionsAndNavigation 以 UserControl 中的一些导航按钮结尾;
<StackPanel Orientation="Horizontal" >
<Button Margin="0,40,40,0"
IsEnabled="{Binding Path=PreviousSectionId, Converter={StaticResource ResourceKey=IntBooleanConverter}}"
Command="{Binding Path=NavigateToPreviousSectionCommand}"><< Save and Previous Section</Button>
<Button Margin="0,40,0,0"
IsEnabled="{Binding Path=NextSectionId, Converter={StaticResource ResourceKey=IntBooleanConverter}}"
Command="{Binding Path=NavigateToNextSectionCommand}">Save and Next Section >></Button>
</StackPanel>
并显示如下;
现在的问题是在绑定路径中:Survey.SelectedSection.QuestionsAndNavigation 我希望命令位于伟大的 GrandParent class SurveyPageViewModel(包含 Survey 对象)而不是嵌入式集合中class 问题和导航。默认情况下,数据上下文查找错误 class。 重要的是要认识到这不是继承。 "Survey" 此处映射到 SurveyPageViewModel class,其中包含映射到 SurveyViewModel class 的 Survey、映射到 SectionViewModel class 的 SelectedSection 以及包含 QuestionsAndNavigation 的 SelectedSection QuestionViewModel 集合 classes.
所以 QuestionViewModel 中的代码:
private void NavigateToNextSection()
{
NavigateToSection(this.NextSectionId);
}
private void NavigateToPreviousSection()
{
NavigateToSection(this.PreviousSectionId);
}
private void NavigateToSection(int sectionId)
{
NavService.Navigate("NewSection", sectionId);
}
不转到 SurveyPageViewModel,我无法在 QuestionViewModel class 中设置 NavService,因为它不存在,它是 SurveyPageViewModel 中的 属性。
编辑。 所以感谢 Eric 的回答,我的 ListView 包含一个 x:Name = "ListResponses"。我在我的按钮路径中使用它:
<StackPanel Orientation="Horizontal" >
<Button Margin="0,40,40,0"
IsEnabled="{Binding Path=PreviousSectionId,
Converter={StaticResource ResourceKey=IntBooleanConverter}}"
Command="{Binding
Path=DataContext.SaveThenPreviousSectionCommand,
ElementName=ListResponses}"><< Previous Section</Button>
<Button Margin="0,40,0,0"
IsEnabled="{Binding Path=NextSectionId,
Converter={StaticResource ResourceKey=IntBooleanConverter}}"
Command="{Binding
Path=DataContext.SaveThenNextSectionCommand,
ElementName=ListResponses}">Next Section >></Button>
</StackPanel>
我已经将事件连接放在 SurveyPageViewModel 中,但由于某种原因它们没有连接,我想知道答案;
public ICommand SaveThenPreviousSectionCommand { get; private set; }
public ICommand SaveThenNextSectionCommand { get; private set; }
... (在构造函数中)
SaveThenPreviousSectionCommand = new DelegateCommand(DoSaveThenPreviousSection);
SaveThenNextSectionCommand = new DelegateCommand(DoSaveThenNextSection);
...
private async void DoSaveThenPreviousSection()
{
var i = 1;
}
private async void DoSaveThenNextSection()
{
var i = 0;
}
编辑:这是页面的完整 XAML 文件
<prism:VisualStateAwarePage x:Class="M.Survey.Views.SurveyPage"
x:Name="pageRoot"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:views="using:M.Survey.Views"
xmlns:local="using:M.Survey"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:prism="using:Microsoft.Practices.Prism.StoreApps"
xmlns:mvvm="using:Microsoft.Practices.Prism.Mvvm"
mvvm:ViewModelLocator.AutoWireViewModel="true"
xmlns:userControls="using:M.Survey.UserControls"
mc:Ignorable="d"
xmlns:converters="using:M.Survey.Converters"
Loaded="pageRoot_Loaded"
Unloaded="pageRoot_Unloaded"
xmlns:templateSelectors="using:M.Survey.TemplateSelectors">
<prism:VisualStateAwarePage.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Themes/AllQuestionDataTemplates.xaml" />
<ResourceDictionary Source="/Themes/Styles.xaml" />
<ResourceDictionary Source="/Themes/DataTemplates.xaml" />
</ResourceDictionary.MergedDictionaries>
<templateSelectors:QuestionTemplateSelector x:Key="QuestionDisplay"
TextEntry="{StaticResource TextEntry}"
DateTimeEntry="{StaticResource DateTimeEntry}"
BoolEntry="{StaticResource BoolEntry}"
IntEntry="{StaticResource IntEntry}"
SelectEntry="{StaticResource SelectEntry}"
DecimalEntry="{StaticResource DecimalEntry}"
LargeTextArea="{StaticResource LargeTextArea}"
SorEntry="{StaticResource SorEntry}"
Additional="{StaticResource AdditionalEntry}"
Miscellaneous="{StaticResource Miscellaneous}"
Dim="{StaticResource Dim}"
Signature="{StaticResource Signature}"
SectionNavigation="{StaticResource SectionNavigation}"/>
<converters:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
<converters:InverseBooleanConverter x:Key="InverseBooleanConverter" />
</ResourceDictionary>
</prism:VisualStateAwarePage.Resources>
<Grid>
<Grid.Background>
<SolidColorBrush Color="{StaticResource ResourceKey=AppBackgroundColor}" />
</Grid.Background>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="80" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid Grid.Row="0" Background="{StaticResource ResourceKey=MulalleyBlueBrush}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<StackPanel Orientation="Horizontal" Margin="20, 0, 0, 5">
<Button Margin="0, 0, 15, 0" Command="{Binding Path=TryGoHomeCommand}" Style="{StaticResource BackButtonStyle}" />
<TextBlock Style="{StaticResource ResourceKey=MediumHeaderTextBlockStyle}" Text="{Binding Path=Job.ShortDisplayAddress}" VerticalAlignment="Bottom" />
<StackPanel Margin="40, 0, 0, 6" Width="30" VerticalAlignment="Bottom" Height="30">
<ProgressRing Height="30" Width="30" Foreground="White" IsActive="{Binding Path=WorkInProgress}" Visibility="{Binding Path=WorkInProgress, Converter={StaticResource ResourceKey=BooleanToVisibilityConverter}}" />
</StackPanel>
<TextBlock Margin="10, 0, 0, 10" VerticalAlignment="Bottom" Text="{Binding Path=FeedbackMsg}" Style="{StaticResource ResourceKey=SmallHeaderTextBlockStyle}" />
</StackPanel>
<Grid Margin="0, 0, 15, 5" Grid.Column="1" VerticalAlignment="Bottom">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock VerticalAlignment="Bottom" Text="Internet" Style="{StaticResource ResourceKey=MediumHeaderTextBlockStyle}" />
<TextBlock Margin="10, 0, 15, 6" VerticalAlignment="Bottom" Grid.Column="1" Text="{Binding Path=ConnectionStatus}" Style="{StaticResource ResourceKey=SmallHeaderStyle}" />
<TextBlock VerticalAlignment="Bottom" Text="Server" Style="{StaticResource ResourceKey=MediumHeaderTextBlockStyle}" Grid.Column="2" />
<TextBlock Margin="10, 0, 15, 6" VerticalAlignment="Bottom" Grid.Column="3" Text="{Binding Path=VpnStatus}" Style="{StaticResource ResourceKey=SmallHeaderStyle}" />
<TextBlock VerticalAlignment="Bottom" Text="User" Style="{StaticResource ResourceKey=MediumHeaderTextBlockStyle}" Grid.Column="4" />
<TextBlock Margin="10, 0, 0, 6" VerticalAlignment="Bottom" Grid.Column="5" Text="{Binding Path=User.DisplayName}" Style="{StaticResource ResourceKey=SmallHeaderStyle}" />
</Grid>
<TextBlock Width="80" Margin="100, 0, 30, 0" Text="{Binding Path=AnswersCompleteDisplay}" Grid.Column="1" Style="{StaticResource ResourceKey=MediumHeaderTextBlockStyle}"/>
<Button Style="{StaticResource ResourceKey=SaveButtonStyle}" Content="Save" Grid.Column="2" Command="{Binding Path=SaveCommand}" />
</Grid>
</Grid>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="2*" />
</Grid.ColumnDefinitions>
<Grid>
<Grid.Background>
<SolidColorBrush Color="{StaticResource ResourceKey=MulalleyBlue}" Opacity=".2" />
</Grid.Background>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ListView
ItemsSource="{Binding Path=Survey.Sections, Mode=TwoWay}"
SelectedItem="{Binding Path=Survey.SelectedSection, Mode=TwoWay}"
IsSwipeEnabled="False"
SelectionMode="Single"
ItemContainerStyle="{StaticResource ResourceKey=ListViewItemContainerStyle}"
ItemTemplate="{StaticResource ResourceKey=SurveySectionDataTemplate}"
ScrollViewer.HorizontalScrollBarVisibility="Hidden"
ScrollViewer.VerticalScrollBarVisibility="Auto"
/>
</Grid>
<Grid Grid.Column="1" Margin="30, 30, 15, 20">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal">
<TextBlock Text="*" FontSize="40" FontWeight="Bold" Foreground="Red"/>
<TextBlock Text=" = Required " FontSize="20"/>
</StackPanel>
<ListView
x:Name="ListResponses"
Grid.Row="1"
ItemsSource="{Binding Path=Survey.SelectedSection.QuestionsAndNavigation, Mode=TwoWay}"
IsSwipeEnabled="False"
SelectionMode="None"
ScrollViewer.VerticalScrollBarVisibility="Auto"
Background="White"
ItemTemplateSelector="{StaticResource ResourceKey=QuestionDisplay}"
ItemContainerStyle=
"{StaticResource ResourceKey=QuestionListViewItemContainerStyle}">
</ListView>
</Grid>
</Grid>
</Grid>
</Grid>
<userControls:SurveyDialogUserControl />
<userControls:SurveyReassignedDialogUserControl />
<userControls:CopyFromUserControl />
</Grid>
<prism:VisualStateAwarePage.BottomAppBar>
<CommandBar Name="commandBar">
<CommandBar.SecondaryCommands>
<AppBarButton Label="Settings" Icon="Setting" Click="AppBarButton_Click" />
<AppBarButton Label="Log" Icon="Admin" Click="LogButton_Click" />
<AppBarButton Label="Refresh" Icon="Refresh" Command="{Binding Path=RefershAllDataCommand}" IsEnabled="{Binding Path=WorkInProgress, Converter={StaticResource ResourceKey=InverseBooleanConverter}}" Visibility="{Binding Path=VpnOnline, Converter={StaticResource ResourceKey=BooleanToVisibilityConverter}}" />
<AppBarButton Label="Copy From" Icon="Copy" Click="CopyButton_Click" Command="{Binding Path=ShowCopyFromCommand}" IsEnabled="{Binding Path=WorkInProgress, Converter={StaticResource ResourceKey=InverseBooleanConverter}}" />
</CommandBar.SecondaryCommands>
</CommandBar>
</prism:VisualStateAwarePage.BottomAppBar>
</prism:VisualStateAwarePage>
这是用户控件 XAML;
<UserControl
x:Class="M.Survey.UserControls.SectionNavigation"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:M.Survey.UserControls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:converters="using:M.Survey.Converters"
mc:Ignorable="d"
d:DesignHeight="30"
d:DesignWidth="400">
<UserControl.Resources>
<ResourceDictionary>
<converters:IntBooleanConverter x:Key="IntBooleanConverter" />
</ResourceDictionary>
</UserControl.Resources>
<Grid>
<StackPanel Orientation="Horizontal" >
<Button Margin="0,40,40,0"
IsEnabled="{Binding Path=PreviousSectionId,
Converter={StaticResource ResourceKey=IntBooleanConverter}}"
Command="{Binding
Path=DataContext.SaveThenPreviousSectionCommand,
ElementName=ListResponses}"><< Previous Section</Button>
<Button Margin="0,40,0,0"
IsEnabled="{Binding Path=NextSectionId,
Converter={StaticResource ResourceKey=IntBooleanConverter}}"
Command="{Binding
Path=DataContext.SaveThenNextSectionCommand,
ElementName=ListResponses}">Next Section >></Button>
</StackPanel>
</Grid>
</UserControl>
假设 ListView XAML 与 Button XAML 在同一个文件中(并且 ListView 是 x:Name="listView"):
<Button Margin="0,40,0,0"
IsEnabled="{Binding Path=NextSectionId, Converter={StaticResource ResourceKey=IntBooleanConverter}}"
Command="{Binding ElementName=listView, Path=DataContext.Survey.DESIRED_COMMAND}">Save and Next Section >></Button>
此外,您还可以在实例化时将调查视图模型的实例传递到问题视图模型中(取决于您创建视图模型的方式)。
最后,您还可以使用事件发布系统(例如 MVVM Light Messenger)在单击按钮时发布一个事件,调查已连线以响应。
我从 Eric 那里得到了很多帮助,Eric 给出了另一个答案。 为了在嵌入在它们自己的复杂设计中的 2 classes 之间进行通信,一种方法是使用 EventAggregator 模式。碰巧我正在开发的应用程序使用了 Prism,它是 EventAggregator 自己的实现。此模式使您能够发布一个有效载荷,该有效载荷由接收 class.
上的订阅事件拾取