单击ItemsControl的行并传递一个id

时间:2016-01-26 15:50:13

标签: c# wpf click row

我制作了一个列表,我的需求是能够在行中点击(触摸)用户,点击后我应该能够通过' mid &# 39;对于事件,我隐藏在代码中,然后点击该记录。 它有可能吗?

<Grid Grid.Row="1" Grid.ColumnSpan="2">
            <ScrollViewer VerticalScrollBarVisibility="Hidden" PanningMode="Both">
                <ItemsControl ItemsSource="{Binding Path=NextMeetingList}">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Grid.Column="0" Grid.Row="0">                                    
                                <Border Grid.Column="0" Grid.Row="0" Margin="10" Height="60" Background="GhostWhite" CornerRadius="3" BorderBrush="{Binding BorderColor}" BorderThickness="0,8,0,0">
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="118"></ColumnDefinition>
                                            <ColumnDefinition Width="*"></ColumnDefinition>
                                        </Grid.ColumnDefinitions>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="60"/>
                                        </Grid.RowDefinitions>
                                        <TextBlock Grid.Row="0" Grid.Column="0" HorizontalAlignment="Left" FontSize="40" Margin="10,5,0,0" Style="{DynamicResource Lato-Semibold}" Text="{Binding endDate.Day}"/>
                                        <TextBlock Grid.Row="0" Grid.Column="0" HorizontalAlignment="Left" FontSize="12" Margin="77,13,0,0" Height="Auto" Style="{DynamicResource Lato-Semibold}" Text="{Binding DayString}"/>
                                        <TextBlock Grid.Row="0" Grid.Column="0" HorizontalAlignment="Left" FontSize="14" Margin="70,26,0,0" Height="Auto" Style="{DynamicResource Lato-Semibold}" Text="{Binding endDate.Hour}"/>
                                        <TextBlock Grid.Row="0" Grid.Column="0" HorizontalAlignment="Left" FontSize="14" Margin="86,26,0,0" Height="Auto" Style="{DynamicResource Lato-Semibold}" Text=":"/>
                                        <TextBlock Grid.Row="0" Grid.Column="0" HorizontalAlignment="Left" FontSize="14" Margin="90,26,0,0" Height="Auto" Style="{DynamicResource Lato-Semibold}" Text="{Binding MinuteString}"/>
                                        <Border Grid.Row="0" Grid.Column="0" Width="1" BorderBrush="#BABABA" Height="40" Margin="115,-6,0,0" BorderThickness="1" HorizontalAlignment="Left"></Border>
                                        <TextBlock Grid.Row="0" Grid.Column="1" HorizontalAlignment="Left" Text="{Binding subject}" FontWeight="Bold" FontSize="17" Margin="10,10,0,0"/>
                                        <TextBlock Grid.Row="0" Grid.Column="1" Width="Auto" TextWrapping="Wrap" HorizontalAlignment="Left" Text="{Binding descr}" FontSize="10"  Margin="20,27,150,0"/>
                                        <TextBlock Grid.Row="0" Grid.Column="1" HorizontalAlignment="Right" Text="{Binding companyName}" FontSize="10" Margin="0,10,30,0"/>
                                        <TextBlock Grid.Row="0" Grid.Column="1" HorizontalAlignment="Right" Text="{Binding Location}" FontSize="10" Margin="0,27,30,0"/>
                                        <TextBlock Grid.Row="0" Grid.Column="1" HorizontalAlignment="Right" Text=">" FontSize="15" VerticalAlignment="Center" Margin="0,-5,10,0"/>
                                        <TextBlock Grid.Row="0" Grid.Column="1" HorizontalAlignment="Right" Text="{Binding mid}" Visibility="Hidden" FontSize="15" VerticalAlignment="Center" Margin="0,-5,10,0"/>
                                    </Grid>
                                </Border>
                            </StackPanel>                                    
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </ScrollViewer>
        </Grid>

2 个答案:

答案 0 :(得分:0)

一种可能的解决方案是将您的内容包装在一个不可见的按钮中,并将id或整个对象作为CommandParameter传递给ViewModel上的Command。

<Button Command="{Binding DataContext.MyCommmand, ElementName=mainGrid}"
    CommandParameter="{Binding}">
           <!-- Content -->
</Button>

答案 1 :(得分:0)

通常,如果我想处理任何事情的Click事件,我只需使用Button并覆盖它的.Template

    

<ItemsControl.ItemTemplate>
    <DataTemplate>
        <Button Template="{StaticResource ContentOnlyTemplate}"
                CommandParameter="{Binding mid}"
                Click/Command=...>
            <StackPanel Grid.Column="0" Grid.Row="0">                                    
                ...
            </StackPanel>
        </Button>
    </DataTemplate>
</ItemsControl>

WPF控件意味着“看起来很”,因此在这种情况下Button控件是理想的,因为它包含处理Click和/或Command / CommandParameter的功能。