简体   繁体   中英

Different ListView item Launch Different Flyout

I am looking for a way to launch different Flyout for different ListViewItem depending on the value on variable Type , and currently the converter is not working.

Let's say I have 1 Converter, 2 Flayouts, and 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>

While the Converter:

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();
    }
}

And the ListView:

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

And the Grid_Tapped Event Handler:

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

Is it possible to launch different Flyout based on the value of Type ?

Thank you!

you could do it using polymorphism based on type

public object ShowAttachedFlyout(FrameworkElement t)
{

}
public object ShowAttachedFlyout( OtherspecificTYpe o)
{

}

And then call it with dynamic

FlyoutBase.ShowAttachedFlyout((dynamic)sender);

After few hours of wandering around stackoverflow, I got quite a good idea to solve it from here :

Instead of using converter, I define which flyout to attach and show it programmatically on 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);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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