在我的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 =“ {Binding Path = 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类SurveyPageViewModel(包含Survey对象)中,而不是在嵌入式集合类QuestionsAndNavigation中。默认情况下,数据上下文在错误的类中查找。重要的是要认识到这不是继承。这里的“调查”映射到SurveyPageViewModel类,该类包含一个映射到SurveyViewModel类的Survey,一个SelectedSection映射到SectionViewModel类,并且包含QuestionsAndNavigation,QuestionAndNavigation是QuestionViewModel类的集合。
因此,QuestionViewModel中的代码如下:
private void NavigateToNextSection()
{
NavigateToSection(this.NextSectionId);
}
private void NavigateToPreviousSection()
{
NavigateToSection(this.PreviousSectionId);
}
private void NavigateToSection(int sectionId)
{
NavService.Navigate("NewSection", sectionId);
}
不会进入SurveyPageViewModel,并且由于不在菜单项中,因此无法在QuestionViewModel类中设置NavService,它是SurveyPageViewModel中的一个属性。
编辑。因此,感谢Eric的回答,我的ListView包含ax: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>
从提供另一个答案的人埃里克(Eric)那里得到了很多帮助,我得到了很多帮助。为了获得两个嵌入到它们自己的复杂设计中的类之间的通信,一种实现方法是使用EventAggregator模式。碰巧的是,我正在开发的应用程序使用Prism,因为它是EventAggregator的自己实现。此模式使您可以发布有效负载,该有效负载由接收类上的订阅事件拾取。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句