我在每个gridview项目上都有一个带有弹出菜单的gridview。由于我需要将每个弹出菜单的ItemsSource绑定到一个集合,这很复杂。因为,menuflyout默认没有ItemsSource属性,所以我试图利用Jerry Nixon在此处发布的解决方案:Why doesn't the Windows 8.1 MenuFlyout have ItemsSource property?
第一次点击gridview项目时,一切正常。但是,当我单击另一个gridview项时,我收到以下错误:“元素已经是另一个元素的子元素。”
我猜测Visual Studio正在抱怨,因为当我点击下一个时,弹出菜单已经附加到第一个网格项,并且它正在尝试重用数据模板。有人可以帮忙吗?
XAML:
<GridView
ItemsSource="{x:Bind ViewModel.SourceForOfferorsList}"
IsItemClickEnabled="True"
IsTapEnabled="True"
IsSwipeEnabled="False"
CanDragItems="False"
SelectionMode="Single"
>
<GridView.ItemTemplate>
<DataTemplate x:DataType="models:SetupCompany" >
<Grid x:Name="MyGrid">
<FlyoutBase.AttachedFlyout>
<Flyout helpers:BindableFlyout.ItemsSource="{Binding ElementName=RankPage,Path=DataContext.SourceForSubfactorsFlyoutList,Mode=TwoWay}">
<helpers:BindableFlyout.ItemTemplate>
<DataTemplate>
<MenuFlyoutItem Text="{Binding Text}" />
</DataTemplate>
</helpers:BindableFlyout.ItemTemplate>
</Flyout>
</FlyoutBase.AttachedFlyout>
<i:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Tapped">
<helpers:OpenFlyoutAction/>
</core:EventTriggerBehavior>
</i:Interaction.Behaviors>
<TextBlock Text="Hello"></TextBlock>
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
OpenFlyoutAction类:
public object Execute(object sender, object parameter)
{
try
{
FlyoutBase.ShowAttachedFlyout((FrameworkElement)sender);
}
catch (Exception e)
{
throw;
}
return null;
}
The Bindable Flyout课程:
public class BindableFlyout : DependencyObject
{
#region ItemsSource
public static IEnumerable GetItemsSource(DependencyObject obj)
{
return obj.GetValue(ItemsSourceProperty) as IEnumerable;
}
public static void SetItemsSource(DependencyObject obj, IEnumerable value)
{
obj.SetValue(ItemsSourceProperty, value);
}
public static readonly DependencyProperty ItemsSourceProperty =
DependencyProperty.RegisterAttached("ItemsSource", typeof(IEnumerable),
typeof(BindableFlyout), new PropertyMetadata(null, ItemsSourceChanged));
private static void ItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{ Setup(d as Windows.UI.Xaml.Controls.Flyout); }
#endregion
#region ItemTemplate
public static DataTemplate GetItemTemplate(DependencyObject obj)
{
return (DataTemplate)obj.GetValue(ItemTemplateProperty);
}
public static void SetItemTemplate(DependencyObject obj, DataTemplate value)
{
obj.SetValue(ItemTemplateProperty, value);
}
public static readonly DependencyProperty ItemTemplateProperty =
DependencyProperty.RegisterAttached("ItemTemplate", typeof(DataTemplate),
typeof(BindableFlyout), new PropertyMetadata(null, ItemsTemplateChanged));
private static void ItemsTemplateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{ Setup(d as Windows.UI.Xaml.Controls.Flyout); }
#endregion
private static async void Setup(Windows.UI.Xaml.Controls.Flyout m)
{
if (Windows.ApplicationModel.DesignMode.DesignModeEnabled)
return;
var s = GetItemsSource(m);
if (s == null)
return;
var t = GetItemTemplate(m);
if (t == null)
return;
var c = new Windows.UI.Xaml.Controls.ItemsControl
{
ItemsSource = s,
ItemTemplate = t,
};
var n = Windows.UI.Core.CoreDispatcherPriority.Normal;
Windows.UI.Core.DispatchedHandler h = () => m.Content = c;
await m.Dispatcher.RunAsync(n, h);
}
}
答案 0 :(得分:1)
我更改了代码中的一些地方,并制作了一个简单的代码示例供您参考:
<GridView x:Name="gridview"
IsItemClickEnabled="True"
IsTapEnabled="True"
IsSwipeEnabled="False"
CanDragItems="False"
SelectionMode="Single">
<GridView.ItemTemplate>
<DataTemplate >
<Grid x:Name="MyGrid">
<FlyoutBase.AttachedFlyout>
<Flyout local:BindableFlyout.ItemsSource="{Binding menuItems}">
<local:BindableFlyout.ItemTemplate>
<DataTemplate>
<MenuFlyoutItem Text="{Binding Text}" />
</DataTemplate>
</local:BindableFlyout.ItemTemplate>
</Flyout>
</FlyoutBase.AttachedFlyout>
<TextBlock Text="{Binding item}"></TextBlock>
<i:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="RightTapped">
<core:InvokeCommandAction Command="{Binding relayCommand}" CommandParameter="{Binding ElementName=MyGrid}"/>
</core:EventTriggerBehavior>
</i:Interaction.Behaviors>
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
List<Test> ls = new List<Test>();
List<MyMenuItem> myMenuItems = new List<MyMenuItem>();
myMenuItems.Add(new MyMenuItem() {Text="menu1" });
myMenuItems.Add(new MyMenuItem() {Text="menu2" });
ls.Add(new Test() {item="item1",menuItems=myMenuItems });
ls.Add(new Test() { item = "item2", menuItems = myMenuItems });
gridview.ItemsSource = ls;
}
}
public class Test:ViewModelBase
{
public string item { get; set; }
public List<MyMenuItem> menuItems { get; set; }
public RelayCommand<object> relayCommand { get; set; }
public Test()
{
relayCommand = new RelayCommand<object>(ShowFlyout);
}
private void ShowFlyout(object obj)
{
FrameworkElement senderElement = obj as FrameworkElement;
FlyoutBase flyoutBase = FlyoutBase.GetAttachedFlyout(senderElement);
flyoutBase.ShowAt(senderElement);
}
}
public class MyMenuItem
{
public string Text { get; set; }
}