0

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

我的按钮命令与我的 ViewModel 的 LookupReferencesCommand 相同。所有数据都来自绑定到包含ObservableCollectionChapterReferencesVerseReferences )的对象类到目前为止,我没有成功地让它工作,非常感谢你的帮助。

谢谢!...

4

1 回答 1

1

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

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

对于我的Button CommandCommandParameter我必须这样做,这是我需要做更多阅读以了解其工作原理的地方:

<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"/>

它的祖先级别我不明白。我假设它是因为我在ListView的第二级有Button 控件

于 2013-09-23T07:23:42.560 回答