C# 使用类似发件人的网格选择列表框项目

C# 使用类似发件人的网格选择列表框项目,c#,wpf,xaml,windows-8,listbox,C#,Wpf,Xaml,Windows 8,Listbox,嗨,我的列表框有问题。当我点击列表框中的项目(文本或图像)时,一切正常,但当我点击项目行中的空白处时,项目未被选中,但我不知道问题出在哪里,因为每一行/项目都像网格一样使用,包含图像和文本,我已将标记设置为绑定在网格中,但如果我点击文本或图像,则项目仅被选中 private void userTapped(object sender, TappedRoutedEventArgs e) { var button = sender as Grid; if (

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

 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>
private void userTapped(对象发送方,tappedroutedventargs e)
{
var按钮=发送方作为网格;
如果(按钮!=null)
{
var subject=MyDatasMessagesUserList.FirstOrDefault(sub=>sub==button.Tag);
if(主题!=null)
{
IdOfChoosenUser=subject.messengfromid;
}
}}

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

1) 删除
网格的第三列


2) 将
矩形
(或任何其他控件)添加到
网格的第三列

将网格包装在ListBoxItem内的数据模板中(并在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>
private void userTapped(对象发送方,tappedroutedventargs e)
{
var按钮=发送者作为ListBoxItem;
如果(按钮!=null)
{
var subject=MyDatasMessagesUserList.FirstOrDefault(sub=>sub==button.Tag);
if(主题!=null)
{
IdOfChoosenUser=subject.messengfromid;
}
}}

嗯,不,我在第3栏有图像,只是我没有把它推到这里。问题出在其他方面,你好,贝诺。我的网格位于listbox内的datateplate中,当我尝试在ListBoxItem上设置tap事件时,它不工作,网格的背景也不工作。我想问题出在发送者身上,因为GridHi,我用代码更新了答案,用ListBoxItem包装了网格,不确定你是怎么做到的。此外,ItemContainer样式有一个填充,当您点击项目的限制时,它会阻止点击被拦截,所以我也删除了它。