Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.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# 如何将项目绑定到ItemsSource?_C#_.net_Wpf_Binding - Fatal编程技术网

C# 如何将项目绑定到ItemsSource?

C# 如何将项目绑定到ItemsSource?,c#,.net,wpf,binding,C#,.net,Wpf,Binding,我想在单独的面板中显示myElement.ContextMenu图标。 我正在尝试这样做: <ItemsControl ItemsSource="{Binding ElementName=myElement, Path=ContextMenu.ItemsSource}"> <ItemsControl.ItemTemplate> <DataTemplate DataType="{x:Type MenuItem}">

我想在单独的面板中显示myElement.ContextMenu图标。 我正在尝试这样做:

<ItemsControl ItemsSource="{Binding ElementName=myElement, Path=ContextMenu.ItemsSource}">
    <ItemsControl.ItemTemplate>
        <DataTemplate DataType="{x:Type MenuItem}">
            <Image Source="{Binding Icon}" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>


但它显示的是菜单项的集合,而不是图像。没有xxx.xaml.cs文件中的任何ViewModels和操作,我如何才能执行此操作。

您正在绑定到
ContextMenu.ItemsSource
,该属性与
ContextMenu.Items

ItemsSource
仅当您将其设置为某个对象集合时才会被设置,如果是这种情况,则您的
ItemsControl
也将绑定到同一个对象集合。除非
项目资源中使用的对象列表绑定到名为
Icon
的属性,否则代码将无法工作

如果您尝试绑定到
ContextMenu.Items
,您将获得
MenuItem
对象的集合,但是UI对象一次只能有一个父对象,因此您的
MenuItems
只能存在于
ContextMenu
ItemsControl
中,而不能同时存在于两者中

一个可能的选项是使用转换器进行绑定,它将获取
上下文菜单中的对象,复制
图标
属性,并返回要显示的图像集合。需要注意的是,在第一次打开
上下文菜单
之前,这是不起作用的,因为在需要时才实际呈现
菜单项

<ItemsControl ItemsSource="{Binding ElementName=MyObjectWithContextMenu, 
    Converter={StaticResource MyConverter}}" />


其中,
MyConverter
获取传递给它的对象,获取对象的
ContextMenu
,循环通过
ContextMenu中的每个
MenuItem
。项
,在
列表中存储
图标
属性的副本,然后返回列表。

您将绑定到
ContextMenu.ItemsSource
,该属性与
ContextMenu.Items

ItemsSource
仅当您将其设置为某个对象集合时才会被设置,如果是这种情况,则您的
ItemsControl
也将绑定到同一个对象集合。除非
项目资源中使用的对象列表绑定到名为
Icon
的属性,否则代码将无法工作

如果您尝试绑定到
ContextMenu.Items
,您将获得
MenuItem
对象的集合,但是UI对象一次只能有一个父对象,因此您的
MenuItems
只能存在于
ContextMenu
ItemsControl
中,而不能同时存在于两者中

一个可能的选项是使用转换器进行绑定,它将获取
上下文菜单中的对象,复制
图标
属性,并返回要显示的图像集合。需要注意的是,在第一次打开
上下文菜单
之前,这是不起作用的,因为在需要时才实际呈现
菜单项

<ItemsControl ItemsSource="{Binding ElementName=MyObjectWithContextMenu, 
    Converter={StaticResource MyConverter}}" />


其中,
MyConverter
获取传递给它的对象,获取对象的
ContextMenu
,循环通过
ContextMenu中的每个
MenuItem
。Items
,在
列表中存储
图标
属性的副本,然后返回列表。

为myElement提供XAML(什么是ContextMenu.ItemsSource)ContextMenu是任何框架元素的上下文菜单。它没有任何特殊的代码。只是MenuItems的集合。如果使用ommit数据类型属性会发生什么?如果在XAML中设置MenuItems,则ItemsSource将为null,不是吗?也许您应该编写Path=ContextMenu.items如果您将数据模板中的
Image
控件替换为
ContentControl
Content={Binding Icon}
,会发生什么情况?为myElement提供XAML(什么是ContextMenu.ItemsSource)ContextMenu是任何FrameworkElement的上下文菜单。它没有任何特殊的代码。只是MenuItems的集合。如果使用ommit数据类型属性会发生什么?如果在XAML中设置MenuItems,则ItemsSource将为null,不是吗?也许您应该编写Path=ContextMenu.items如果用
ContentControl
替换数据模板中的
Image
控件,会发生什么情况?