C# 不同的ListView项目启动不同的弹出按钮
我正在寻找一种方法,根据变量C# 不同的ListView项目启动不同的弹出按钮,c#,windows-runtime,winrt-xaml,flyout,C#,Windows Runtime,Winrt Xaml,Flyout,我正在寻找一种方法,根据变量Type上的值,为不同的ListViewItem启动不同的弹出按钮,但当前转换器不工作 假设我有1个转换器、2个扇形图和1个数据模板 <Page.Resources> <common:TypeToFlyoutTypeConverter x:Key="typeToFlyoutConverter"/> <Flyout x:Name="FlyoutTemplateConfirmed"> <Grid &
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>
以及ListView:
<ListView
SelectionMode="None" IsItemClickEnabled="True" IsSwipeEnabled="false"
ItemTemplate="{StaticResource ListViewItemTemplate}"
ItemsSource="{Binding}">
</ListView>
是否可以根据类型的值启动不同的弹出按钮
谢谢大家! 您可以使用基于类型的多态性来实现
public object ShowAttachedFlyout(FrameworkElement t)
{
}
public object ShowAttachedFlyout( OtherspecificTYpe o)
{
}
然后用dynamic调用它
FlyoutBase.ShowAttachedFlyout((dynamic)sender);
在stackoverflow周围徘徊了几个小时后,我有了一个很好的解决方案:
我没有使用转换器,而是定义要附加的弹出按钮,并以编程方式将其显示在网格上
:
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);
}
很抱歉把这个留了一会儿。嗯。。。你能提供一个如何使用它的示例代码吗?我已经尝试过你的例子,但还没有得到有效的结果:(
FlyoutBase.ShowAttachedFlyout((dynamic)sender);
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);
}