不同的ListView项目启动不同的弹出窗口

时间:2015-01-10 16:47:22

标签: c# windows-runtime winrt-xaml flyout

我正在寻找一种方法来为不同的ListViewItem启动不同的Flyout,具体取决于变量Type的值,目前转换器无法正常工作。

我们说我有1个转换器,2个Flayouts和1个DataTemplate。

<Page.Resources>
    <common:TypeToFlyoutTypeConverter x:Key="typeToFlyoutConverter"/>
    <Flyout x:Name="FlyoutTemplateConfirmed">
        <Grid >
            <TextBlock Text="Confirmed"/>
        </Grid>
    </Flyout>
    <Flyout x:Name="FlyoutTemplateRejected">
        <Grid >
            <TextBlock Text="Rejected"/>
        </Grid>
    </Flyout>
    <DataTemplate x:Key="ListViewItemTemplate">
        <Grid Tapped="Grid_Tapped" 
              FlyoutBase.AttachedFlyout="{Binding Type, Converter={StaticResource typeToFlyoutConverter}}">
        </Grid>
    </DataTemplate>
</Page.Resources>

转换器:

public class TypeToFlyoutTypeConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        if ((value as String).Contains("TypeConfirmed"))
            return "StaticResource FlyoutTemplateConfirmed";
        else
            return "StaticResource FlyoutTemplateRejected";
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotSupportedException();
    }
}

ListView:

<ListView
    SelectionMode="None" IsItemClickEnabled="True" IsSwipeEnabled="false"
    ItemTemplate="{StaticResource ListViewItemTemplate}"
    ItemsSource="{Binding}">
</ListView>

Grid_Tapped事件处理程序:

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

是否可以根据Type

的值启动不同的Flyout

谢谢!

2 个答案:

答案 0 :(得分:0)

你可以使用基于类型

的多态来做到这一点
public object ShowAttachedFlyout(FrameworkElement t)
{

}
public object ShowAttachedFlyout( OtherspecificTYpe o)
{

}

然后用动态

调用它
FlyoutBase.ShowAttachedFlyout((dynamic)sender);

答案 1 :(得分:0)

在stackoverflow上闲逛几个小时后,我从here得到了很好的解决方法:

我没有使用转换器,而是定义要附加哪个弹出按钮并在Grid_Tapped上以编程方式显示:

    private void Grid_Tapped(object sender, TappedRoutedEventArgs e)
    {
        string itemType = ((sender as Grid).DataContext as blabla).Type;

        if (itemType.Contains("blibli") && (sender as FrameworkElement) != null)            
            FlyoutBase.SetAttachedFlyout(sender as FrameworkElement, (Flyout)this.Resources["FlyoutTemplateConfirmed"]);
        else if ((sender as FrameworkElement) != null)            
            FlyoutBase.SetAttachedFlyout(sender as FrameworkElement, (Flyout)this.Resources["FlyoutTemplateRejected"]);

        FlyoutBase.ShowAttachedFlyout(sender as FrameworkElement);
    }