我有一个相当复杂的数据绑定模板,我无法回到我的ViewModel来访问属性和命令。
这就是我的xaml从上到下设置为其布局的方式:
<HierarchicalDataTemplate x:Key="ChapterReferencesTemplate">
<StackPanel>
<DockPanel>
<TextBlock Text="Chapter Reference "/>
<AccessText Text="{Binding Path=Chapter}" />
</DockPanel>
<DockPanel>
<TextBlock Text="Total Reference Verses "/>
<AccessText Text="{Binding Path=Verses}" />
</DockPanel>
<ListView Name="VerseReferencesListView" Height="200"
ItemsSource="{Binding Path=VerseReferences}"
SelectedItem="{Binding Path=DataContext.CurrentVerseReference, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type src:CreateWritingsViewModel}}}"
AlternationCount="2"
BorderThickness="0"
IsSynchronizedWithCurrentItem="True">
<ListView.View>
<GridView AllowsColumnReorder="true" ColumnHeaderToolTip="xmlNamespace List" >
<GridViewColumn
Header="Verse"
DisplayMemberBinding="{Binding Path=Verse}" Width="Auto">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Width="Auto" TextAlignment="Left" Text="{Binding}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn
Header="Query"
DisplayMemberBinding="{Binding Path=Query}" Width="Auto">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Width="Auto" TextAlignment="Left" Text="{Binding}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Actions">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button
Command="{Binding Path=DataContext.LookupReferencesCommand, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type src:CreateWritingsViewModel}}}"
CommandParameter="{Binding .}"
Template="{StaticResource AddButtonTemplate}" Cursor="Hand" Width="30"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</StackPanel>
<HierarchicalDataTemplate x:Key="WritingsReferenceTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="200"/>
</Grid.ColumnDefinitions>
<GroupBox Header="{Binding Path=Hebrew}">
<StackPanel Margin="5 10 5 0">
<StackPanel>
<DockPanel>
<TextBlock Text="Writings of "/>
<AccessText Text="{Binding Path=Writings}" />
</DockPanel>
<DockPanel>
<TextBlock Text="Total Reference Chapters : "/>
<AccessText Text="{Binding Path=Chapters}" />
</DockPanel>
<DockPanel>
<TextBlock Text="Total Reference Verses : "/>
<AccessText Text="{Binding Path=Verses}" />
</DockPanel>
<DockPanel >
<TextBlock Text="Query for Writing : "/>
<AccessText Text="{Binding Path=QueryWriting}" />
</DockPanel>
<DockPanel >
<TextBlock Text="Query for Chapters : "/>
<AccessText Text="{Binding Path=QueryChapters}" />
</DockPanel>
</StackPanel>
<DockPanel Margin="0 5 0 0">
<GroupBox Header="Chapter References">
<Expander>
<ScrollViewer VerticalScrollBarVisibility="Auto" Height="200">
<HeaderedItemsControl
ItemTemplate="{StaticResource ChapterReferencesTemplate}"
ItemsSource="{Binding Path=ChapterReferences}"
Margin="10,0,0,0" />
</ScrollViewer>
</Expander>
</GroupBox>
</DockPanel>
</StackPanel>
</GroupBox>
</Grid>
<GroupBox Grid.Column="1" Header="CREATE REFERENCES">
<ListBox
Name="ReferenceListBox"
ItemTemplate="{StaticResource WritingsReferenceTemplate}"
ItemsSource="{Binding Path=odsDocumentsService.WritingsReferenceItems}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
VirtualizingStackPanel.IsVirtualizing="False"
VirtualizingStackPanel.VirtualizationMode="Standard">
</ListBox>
在我的HierarchicalDataTemplate x:Key="ChapterReferencesTemplate"中,您可以看到我已经绑定到ItemsSource="{Binding Path=VerseReferences}"但我还需要为ListView的SelectedItem属性绑定到ViewModel 的 CurrentVerseReference。
我的按钮命令与我的 ViewModel 的 LookupReferencesCommand 相同。所有数据都来自绑定到包含ObservableCollection(ChapterReferences,VerseReferences )的对象类到目前为止,我没有成功地让它工作,非常感谢你的帮助。
谢谢!...