繁体   English   中英

ListView中的MenuFlyout:单击了哪个元素

[英]MenuFlyout in a ListView: which element has been clicked

我有一个带有评论列表的ListView:

<ListView ItemsSource="{Binding Comments}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <Border Background="{Binding User, Converter={StaticResource UsernameToBackgroundColorConverter}}"
                    Margin="0,5" 
                    HorizontalAlignment="Stretch"
                    FlyoutBase.AttachedFlyout="{StaticResource FlyoutBase1}"
                    Holding="BorderCommento_Holding">
                    <StackPanel>
                        <Grid Margin="5">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>
                        <TextBlock Text="{Binding User}"
                                       FontSize="20"
                                       Grid.Column="0"
                                       FontWeight="Bold"
                                       Style="{ThemeResource ListViewItemSubheaderTextBlockStyle}"/>
                        <TextBlock HorizontalAlignment="Right"
                                       Text="{Binding DateTime}"
                                       FontSize="20"
                                       Grid.Column="1"
                                       Style="{ThemeResource ListViewItemSubheaderTextBlockStyle}"/>
                    </Grid>
                    <TextBlock Margin="5,0,5,5"
                                       Text="{Binding Text}"
                                       FontSize="20"
                                       TextWrapping="Wrap"/>
                </StackPanel>
            </Border>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

评论类别:

public class Comment
{
    public Comment(String id, String user, String text, String date_time)
    {
        this.Id = id;
        this.User = user;
        this.Text = text;
        this.DateTime = date_time;
    }

    public string Id { get; private set; }
    public string User { get; private set; }
    public string Text { get; private set; }
    public string DateTime { get; private set; }
}

按住注释时出现的弹出菜单在Page.Resources中定义:

<Page.Resources>
    <MenuFlyout x:Name="flyout1" x:Key="FlyoutBase1">
        <MenuFlyoutItem x:Name="ReportCommentFlyout" 
                        Text="{Binding User, Converter={StaticResource ReportOrDeleteComment}}" 
                        Click="ReportCommentFlyout_Click"/>
    </MenuFlyout>
</Page.Resources>

现在,在ReportCommentFlyout_Click中,我需要知道正在报告/删除的注释ID。 我该怎么做?

我试过了

string CommentId = ((Comment)e.OriginalSource).Id;

但是应用程序崩溃了...

您的应用程序崩溃是因为您将e.OriginalSource投射为Comment,但由于它不是该类型而无法使用。 通常,使用“ as”进行此操作通常更安全

var comment = someObject as Comment;
if (comment != null)
{

....

}

关于您的问题,您是否尝试过

var menuFlyoutItem = sender as MenuFlyoutItem;
if (menuFlyoutItem != null)
{
    var comment = menuFlyoutItem.DataContext as Comment;
    if (comment != null)
    {
        string CommentId = comment.Id;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM