Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何绑定MenuFlyout“;itemsource";当页面上有多个弹出按钮时是否使用collection?_C#_Xaml_Uwp - Fatal编程技术网

C# 如何绑定MenuFlyout“;itemsource";当页面上有多个弹出按钮时是否使用collection?

C# 如何绑定MenuFlyout“;itemsource";当页面上有多个弹出按钮时是否使用collection?,c#,xaml,uwp,C#,Xaml,Uwp,我有一个gridview,每个gridview项目上都有一个弹出式菜单。由于我需要将每个弹出菜单的ItemsSource绑定到一个集合,所以这一点很复杂。由于默认情况下menuflyout没有ItemsSource属性,因此我尝试使用Jerry Nixon在此处发布的解决方案: 当我第一次单击gridview项目时,一切正常。但是,当我单击另一个gridview项目时,我得到以下错误:“元素已经是另一个元素的子元素。” 我猜Visual Studio正在抱怨,因为当我单击下一个网格项时,弹出式菜

我有一个gridview,每个gridview项目上都有一个弹出式菜单。由于我需要将每个弹出菜单的ItemsSource绑定到一个集合,所以这一点很复杂。由于默认情况下menuflyout没有ItemsSource属性,因此我尝试使用Jerry Nixon在此处发布的解决方案:

当我第一次单击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>
可绑定弹出型按钮类:

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

我更改了代码中的一些位置,并制作了一个简单的代码示例供您参考:

<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>

公共密封部分类主页面:第页
{
公共主页()
{
this.InitializeComponent();
列表ls=新列表();
List myMenuItems=新列表();
添加(新的MyMenuItem(){Text=“menu1”});
添加(新的MyMenuItem(){Text=“menu2”});
添加(newtest(){item=“item1”,menuItems=myMenuItems});
添加(newtest(){item=“item2”,menuItems=myMenuItems});
gridview.ItemsSource=ls;
}
}
公共类测试:ViewModelBase
{
公共字符串项{get;set;}
公共列表菜单项{get;set;}
公共RelayCommand RelayCommand{get;set;}
公开考试()
{
relayCommand=新的relayCommand(显示弹出按钮);
}
专用空心显示弹出按钮(对象obj)
{
FrameworkElement senderElement=obj作为FrameworkElement;
FlyoutBase FlyoutBase=FlyoutBase.GetAttachedFlyout(senderElement);
飞出基地。ShowAt(senderElement);
}
}
公共类MyMenuItem
{
公共字符串文本{get;set;}
}

太棒了!!!成功了。我还尝试在ViewModel中实现这一点,以便更接近MVVM,但没有成功。但它在代码背后工作得很好。非常感谢!
<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>