0

嗨,我的列表框有一点问题。当我单击列表框中的项目(文本或图像)时,一切正常,但是当我单击项目行中的空白区域时,未选择项目,但我不知道哪里有问题,因为每一行/项目都像包含的网格一样使用图像和文本,我已将标签设置为网格中的绑定,但如果我单击文本或图像,则仅选择项目。

 private void userTapped(object sender, TappedRoutedEventArgs e)
    {
        var button = sender as Grid;
        if (button != null)
        {
            var subject = MyDatasMessagesUserList.FirstOrDefault(sub => sub == button.Tag);
            if (subject != null)
            {
                IdOfChoosenUser = subject.MessengeFromId;
            }
        }}


<ListBox x:Name="lbMessagesUsersList" Grid.Column="1" FontSize="13"   ItemsSource="{Binding MyDatasMessagesUserList }" Margin="0,0,0,80">
            <ListBox.ItemContainerStyle>
                <Style TargetType="ListBoxItem">
                    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                </Style>
            </ListBox.ItemContainerStyle>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid VerticalAlignment="Top" Margin="0,0,0,0" Tapped="userTapped"  Tag="{Binding}" >
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <Grid Grid.Row="0">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="auto"/>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <Image Grid.Column="0" Source="layoutGraphics/logo120.png" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="5,5,0,0"></Image>
                            <TextBlock x:Name="tbMessengerName" Text="{Binding MessengerName}" HorizontalAlignment="Left"
                                                   Foreground="#FF02416C"
                                                   FontSize="16" Grid.Column="1" Margin="10,0,20,0" VerticalAlignment="Center" MinWidth="120"/>
                            <Image x:Name="imgMessengerIsOffline" 
                                                   HorizontalAlignment="Right" 
                                                    Margin="10,0,20,0" Grid.Column="2" VerticalAlignment="Center"  Source="{Binding isUserOnline}"  Width="20" Height="20">
                            </Image>
                        </Grid>
                        <Rectangle Fill="#FF6FB7FF" Grid.Row="1"  Height="1"                          Margin="0,0,0,-20">
                        </Rectangle>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
4

2 回答 2

1

将网格包装在 ListBoxItem 内的 DataTemplate 中(并在 ListBoxItem 上设置点击事件)或将网格的背景设置为透明,并且点击事件应该可以正常工作。

private void userTapped(object sender, TappedRoutedEventArgs e)
{
    var button = sender as ListBoxItem;
    if (button != null)
    {
        var subject = MyDatasMessagesUserList.FirstOrDefault(sub => sub == button.Tag);
        if (subject != null)
        {
            IdOfChoosenUser = subject.MessengeFromId;
        }
    }}


<ListBox x:Name="lbMessagesUsersList" Grid.Column="1" FontSize="13"   ItemsSource="{Binding MyDatasMessagesUserList }" Margin="0,0,0,80">
        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                <Setter Property="Padding" Value="0"/>
            </Style>
        </ListBox.ItemContainerStyle>
        <ListBox.ItemTemplate>
            <DataTemplate>
               <ListBoxItem Tapped="userTapped"  Tag="{Binding}">
                <Grid VerticalAlignment="Top" Margin="8,10"  >
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <Grid Grid.Row="0">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="auto"/>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <Image Grid.Column="0" Source="layoutGraphics/logo120.png" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40" Height="40" Margin="5,5,0,0"></Image>
                        <TextBlock x:Name="tbMessengerName" Text="{Binding MessengerName}" HorizontalAlignment="Left"
                                               Foreground="#FF02416C"
                                               FontSize="16" Grid.Column="1" Margin="10,0,20,0" VerticalAlignment="Center" MinWidth="120"/>
                        <Image x:Name="imgMessengerIsOffline" 
                                               HorizontalAlignment="Right" 
                                                Margin="10,0,20,0" Grid.Column="2" VerticalAlignment="Center"  Source="{Binding isUserOnline}"  Width="20" Height="20">
                        </Image>
                    </Grid>
                    <Rectangle Fill="#FF6FB7FF" Grid.Row="1"  Height="1"                          Margin="0,0,0,-20">
                    </Rectangle>
                </Grid>
             </ListBoxItem>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
于 2013-09-12T13:10:15.453 回答
0

我认为你的问题是因为你有 3 列,Grid但你只使用了其中的两列。因此,没有可供您单击的对象,因此该项目不会被选中。我想您可以通过以下方式解决您的问题

1)删除第三列Grid

或者

2) 将一个Rectangle(或任何其他控件)添加到Grid.

于 2013-09-12T08:43:45.393 回答