如何单击ListView项目中的按钮?

时间:2016-04-15 13:59:54

标签: c# wpf xaml mvvm

我有一个像这样的ListView:

$ scrapy shell https://www.upwork.com/job/Education-portal-development_~0151e2b32662a05e13/

>>> for item in response.xpath("//p[strong = 'About the Client']/following-sibling::p"):
...     print(" ".join(map(unicode.strip, item.xpath(".//text()").extract())))
... 
 India  Bangalore
            07:28 PM 
 3
        Jobs Posted   0% Hire Rate,
        1 Open Job 

并且列表的项目定义如下:

<ListView  x:Name="Thumbnails"   HorizontalContentAlignment ="Left" VerticalContentAlignment="Top" Padding="0"  Background ="#81AFD3" Grid.Row="6" Grid.Column="6" Grid.ColumnSpan="10" Grid.RowSpan="27" ScrollViewer.VerticalScrollBarVisibility="Visible" ItemsSource="{Binding NumberOfSlides}" SelectedItem="{Binding SelectedItem}">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="SelectionChanged">
            <i:InvokeCommandAction Command="{Binding ThumbnailsSelectionChangedCommand}" CommandParameter="{Binding Thumbnails.SelectedIndex, Mode=OneWayToSource}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>


    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <Setter Property="Content" Value="{StaticResource Thumbnail}" />
            <Setter Property="Margin" Value="{Binding ActualWidth, ElementName=DummyRectangle2}"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="IsSelected" Value="True"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </ListView.ItemContainerStyle>

    <ListView.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" 
         Color="Blue"/>
    </ListView.Resources>
</ListView>

和ViewModel:

<Grid x:Key="Thumbnail" Width="{Binding ActualWidth, ElementName=DummyRectangle}" Height="{Binding ActualHeight, ElementName=DummyRectangle}" HorizontalAlignment="Center" VerticalAlignment="Top" Background="Transparent" >

    <Grid.RowDefinitions>
        <RowDefinition Height="1*" />
        <RowDefinition Height="1*" />
        <RowDefinition Height="1*" />
        <RowDefinition Height="1*" />
        <RowDefinition Height="1*" />
        <RowDefinition Height="1*" />
        <RowDefinition Height="1*" />
    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="1*" />
        <ColumnDefinition Width="1*" />
        <ColumnDefinition Width="1*" />
        <ColumnDefinition Width="1*" />
        <ColumnDefinition Width="1*" />
        <ColumnDefinition Width="1*" />
        <ColumnDefinition Width="1*" />
        <ColumnDefinition Width="1*" />
        <ColumnDefinition Width="1*" />
        <ColumnDefinition Width="1*" />
        <ColumnDefinition Width="1*" />
        <ColumnDefinition Width="1*" />
        <ColumnDefinition Width="1*" />
        <ColumnDefinition Width="1*" />
    </Grid.ColumnDefinitions>

    <Image Name="thumbImage" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="14" Grid.RowSpan="7" Stretch="UniformToFill" Source="/MS_Show_Assets/ImportAssets/Test.jpg"></Image>

    <StackPanel Name="Overlay" Grid.Row="0" Grid.Column="10" Grid.ColumnSpan="4" Grid.RowSpan="7" Background="LightBlue" Opacity="0.4"/>


    <Button Name="slide_ON_OFF" Grid.Row="0" Grid.Column="10" Grid.ColumnSpan="4" Grid.RowSpan="4" VerticalAlignment="Center" Background="Transparent" Command="{Binding SlideOnOffCommand}">
        <Button.Template>
            <ControlTemplate>
                <Grid RenderTransformOrigin="0.5,0.5" x:Name="bg">
                    <Image Source="/MS_Show_Assets/ImportAssets/Visible_ON.png"/>
                </Grid>
            </ControlTemplate>
        </Button.Template>

    </Button>

    <Button Name="fadein_ON_OFF" Grid.Row="3" Grid.Column="10" Grid.ColumnSpan="4" Grid.RowSpan="4" VerticalAlignment="Center" Background="Transparent">
    <Button.Template>
        <ControlTemplate>
            <Grid RenderTransformOrigin="0.5,0.5" x:Name="bg">
                <Image x:Name ="main_image" Source="/MS_Show_Assets/ImportAssets/Bulletpoint_ON.png"/>
            </Grid>
        </ControlTemplate>
    </Button.Template>
</Button>

如您所见,列表项包含一些按钮。我将其中一个连接到ViewModel中的命令,但是当单击时,订阅的方法不会被调用。如何使这些按钮可以点击?

2 个答案:

答案 0 :(得分:1)

您必须在Button-Command的Command-Binding中提供RelativeSource

你的命令应该看起来像:

Command="{Binding DataContext.ThumbnailsSelectionChangedCommand, RelativeSource={RelativeSource FindAncestor, AncestorType=Window} }"

如果你的xaml的root-Element是UserControl而不是一个窗口,那么绑定应该是这样的:

Command="{Binding DataContext.ThumbnailsSelectionChangedCommand, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl} }"

答案 1 :(得分:0)

这解决了我的问题:

<Button Name="slide_ON_OFF" Grid.Row="0" Grid.Column="10" Grid.ColumnSpan="4" Grid.RowSpan="4" VerticalAlignment="Center" Background="Transparent" Command="{Binding DataContext.SlideOnOffCommand, RelativeSource={RelativeSource FindAncestor, AncestorType=ListView}}"/>