从列表视图中查找视图模型属性时出现问题



我有一个相当复杂的数据绑定模板,我无法找到回到我的 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}",但我还需要绑定到我的 ViewModelCurrentVerseReference for ListViewSelectedItem 属性。

我的按钮命令与我的视图模型的查找引用命令相同。 所有数据都来自绑定到包含 ObservableCollection 的对象ChapterReferencesVerseReferences) 到目前为止,我还没有成功让它工作,非常感谢您的帮助。

谢谢!。。。

好吧,我找到了正确的属性来使用它。 但是,我需要做一些阅读以了解有关这些属性设置的更多信息。对于我的选定项,我必须这样做:

SelectedItem="{Binding Path=DataContext.CurrentVerseReference, RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}"

对于我的按钮命令和命令参数,我必须这样做,这是我需要做更多阅读以了解它是如何工作的:

<Button Command="{Binding Path=DataContext.LookupReferencesCommand, RelativeSource={RelativeSource AncestorType={x:Type ListBox}, AncestorLevel=2, Mode=FindAncestor}}" 
                                        CommandParameter="{Binding Path=DataContext.CurrentVerseReference, RelativeSource={RelativeSource AncestorType={x:Type ListBox}, AncestorLevel=2, Mode=FindAncestor}}" 
                                        Template="{StaticResource AddButtonTemplate}"  Cursor="Hand" Width="30"/>

这是祖先级别我不明白。 我假设它是因为我在列表视图的第二级有按钮控件

相关内容

  • 没有找到相关文章

最新更新