将多个字段绑定到 MenuFlyoutItem

Binding multiple fields to MenuFlyoutItem

在我的 Windows/Windows Phone 通用应用程序中,我的 XAML 布局中有 ListViewMenuFlyoutListView 包含人员列表。点击列表项会显示弹出窗口,其中包含发送电子邮件或打电话给该人的选项。现在我正在使用 MenuFlyoutItemTag 属性 来保存电子邮件地址和 phone 号码,但我也希望能够获得此人的姓名这样我就可以在打开电子邮件编辑器或 phone 拨号程序时发送它。

我正在使用 Tag 元素来存储基本信息,但我想访问名称。我该怎么做呢?使用额外的属性?以某种方式访问​​父对象的数据绑定?

XAML:

<ListView
            x:Name="itemPositions"
            AutomationProperties.AutomationId="ItemListView"
            AutomationProperties.Name="Items In Group"
            TabIndex="1"
            Grid.Row="1"
            ItemsSource="{Binding Positions}"
            SelectionMode="None"
            IsSwipeEnabled="false"
            Margin="0,0,0,0">
    <ListView.ItemTemplate>
        <DataTemplate>
            <Grid Tapped="Grid_Tapped">
                <FlyoutBase.AttachedFlyout>
                    <MenuFlyout>
                        <MenuFlyoutItem IsEnabled="{Binding IsFilled}" Tag="{Binding Email}" x:Name="sendEmail" Text="email" Click="sendEmail_Click" />
                        <MenuFlyoutItem IsEnabled="{Binding IsFilled}" Tag="{Binding Phone}" x:Name="sendCall" Text="call" Click="sendCall_Click" />
                    </MenuFlyout>
                </FlyoutBase.AttachedFlyout>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="15"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>

                <Border Grid.Column="0" Visibility="{Binding IsVerified, Converter={StaticResource BoolToInvisible}}" Margin="0,0,0,5" Background="Firebrick" Width="10" Height="70" />
                <Border Visibility="{Binding Required, Converter={StaticResource BoolToVisible}}">
                    <Border Grid.Column="0" Visibility="{Binding IsFilled, Converter={StaticResource BoolToInvisible}}" Margin="0,0,0,5" Background="Gold" Width="10" Height="70" />
                </Border>
                <StackPanel Margin="10,0,0,10" Grid.Column="1">
                    <TextBlock Text="{Binding PositionName}" Style="{ThemeResource ListViewItemTextBlockStyle}"/>
                    <TextBlock Text="{Binding DisplayName}" Style="{ThemeResource ListViewItemSubheaderTextBlockStyle}" />
                </StackPanel>
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

C#:

private void Grid_Tapped(object sender, TappedRoutedEventArgs e)
{
    FrameworkElement element = sender as FrameworkElement;
    if (element != null) FlyoutBase.ShowAttachedFlyout(element);
}

private async void sendEmail_Click(object sender, RoutedEventArgs e)
{
    FrameworkElement element = sender as FrameworkElement;
    Windows.ApplicationModel.Email.EmailMessage mail = new Windows.ApplicationModel.Email.EmailMessage();
    mail.Subject = "Leadsheet Position Assignment";
    mail.To.Add(new Windows.ApplicationModel.Email.EmailRecipient(element.Tag.ToString()));
    await Windows.ApplicationModel.Email.EmailManager.ShowComposeNewEmailAsync(mail);
}

private void sendCall_Click(object sender, RoutedEventArgs e)
{
    FrameworkElement element = sender as FrameworkElement;
    Windows.ApplicationModel.Calls.PhoneCallManager.ShowPhoneCallUI(element.Tag.ToString(), "");
}

我认为您应该能够做到这一点,例如通过检查单击的 GridMenuItem 的 DataContext :

private void Grid_Tapped(object sender, TappedRoutedEventArgs e)
{
    Item yourItem = (sender as Grid).DataContext as Item;
    FrameworkElement element = sender as FrameworkElement;
    if (element != null) FlyoutBase.ShowAttachedFlyout(element);
}

// you can also do the same in your menu items:

private async void sendEmail_Click(object sender, RoutedEventArgs e)
{
    Item yourItem = (sender as MenuFlyoutItem).DataContext as Item;
    FrameworkElement element = sender as FrameworkElement;
    Windows.ApplicationModel.Email.EmailMessage mail = new Windows.ApplicationModel.Email.EmailMessage();
    mail.Subject = "Leadsheet Position Assignment";
    mail.To.Add(new Windows.ApplicationModel.Email.EmailRecipient(element.Tag.ToString()));
    await Windows.ApplicationModel.Email.EmailManager.ShowComposeNewEmailAsync(mail);
}

Item 以上是您在 Positions 中使用的 ItemClass 设置为 ItemsSource。一旦你点击了项目,剩下的就很容易了。有了这个,您也不再需要绑定 Tag 属性.