不同的列表视图项 启动不同的浮出控件
本文关键字:控件 视图 列表 启动 | 更新日期: 2023-09-27 18:37:13
我正在寻找一种方法,根据变量Type
的值为不同的 ListViewItem 启动不同的浮出控件,目前转换器不起作用。
假设我有 1 个转换器、2 个布局和 1 个数据模板。
<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
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
的值启动不同的浮出控件?
谢谢!
您可以使用基于类型的多态性来做到这一点
public object ShowAttachedFlyout(FrameworkElement t)
{
}
public object ShowAttachedFlyout( OtherspecificTYpe o)
{
}
然后用动态调用它
FlyoutBase.ShowAttachedFlyout((dynamic)sender);
在stackoverflow周围徘徊了几个小时后,我有一个很好的主意从这里解决它:
我没有使用转换器,而是定义要附加的浮出控件并以编程方式在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);
}