绑定双击嵌套列表视图

时间:2018-01-22 11:06:55

标签: c# wpf mvvm-light

我有一个ListView嵌套到另一个Listview中。现在我想将双击事件绑定到内部ListView的ListViewItems

<UserControl>
    <UserControl.Resources>
        <DataTemplate x:Key="DefaultTemplate">
            <ListView Name="jobsView" ItemsSource="{Binding jobs}" SelectedItem="{Binding Path=SelectedProduction}" >
                <ListView.InputBindings>
                    <MouseBinding Gesture="LeftDoubleClick" Command="{Binding Path=DataContext.ItemSelectedCommand, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" CommandParameter="{Binding ElementName=jobsView, Path=SelectedItem}" />
                 </ListView.InputBindings>
             </ListView>
         </DataTemplate>
    </UserControl.Resources>
    <ListView Name="weekView" ItemsSource="{Binding dayList}" ItemTemplate="{StaticResource DefaultTemplate}"  >
    </ListView>
</UserControl>

我在ViewModel中创建了一个名为ItemSelectedCommand的RelayCommand。

public RelayCommand ItemSelectedCommand { get; private set; }

RelayCommand没有被触发。我想我正在设置错误的RelativeSource。它看起来怎么样?

2 个答案:

答案 0 :(得分:0)

您的ListView在哪里插入。在Visual Tree中是否存在类型为UserControl的父级?

另外,修复Binding错误的好处是看看控制台。应该有绑定错误可能会指出你是错误的。通常是写下它试图搜索对象和属性的地方:)

另外,我不确定绑定到属性时是否实际允许private get;

答案 1 :(得分:0)

<StackPanel Grid.Column="1">
        <StackPanel.Resources>
            <DataTemplate x:Key="DefaultTemplate" DataType="{x:Type sys:String}">
                <StackPanel>
                    <TextBlock Text="{Binding .}"/>
                    <ListView>
                        <ListView.ItemsSource>
                            <CompositeCollection>
                                <sys:String>Sub Item</sys:String>
                            </CompositeCollection>
                        </ListView.ItemsSource>
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding}">
                                    <TextBlock.InputBindings>
                                        <MouseBinding Gesture="LeftDoubleClick" Command="{Binding DataContext.RenameCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MainWindow}}, PresentationTraceSources.TraceLevel=High}" CommandParameter="{Binding SelectedItem, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}}"/>
                                    </TextBlock.InputBindings>
                                </TextBlock>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
                </StackPanel>
            </DataTemplate>
        </StackPanel.Resources>

        <ListView ItemTemplate="{StaticResource DefaultTemplate}">
            <ListView.ItemsSource>
                <CompositeCollection>
                    <sys:String> First Item</sys:String>
                </CompositeCollection>
            </ListView.ItemsSource>
        </ListView>
    </StackPanel>  

为什么它不起作用的原因是因为双击是在实际的ListViewItem而不是ListView。