来自 xaml 的 WPF 控件引用在代码端不可见
<ContextMenu x:Key="EffectsContext"
Name="EffectsMenu"
StaysOpen="true">
<MenuItem Header="Add Blur">
<MenuItem.Icon>
<Image Width="16"
Height="16"
Source="{Binding Source={x:Static prop:Resources.BlurIcon},
Converter={StaticResource BitmapToImageSourceConverter}}" />
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="Add Fractal">
<MenuItem.Icon>
<Image Width="16"
Height="16"
Source="{Binding Source={x:Static prop:Resources.Fractalcon},
Converter={StaticResource BitmapToImageSourceConverter}}" />
</MenuItem.Icon>
</MenuItem>
</ContextMenu>
在我的 MainWindow.xaml.cs
文件中无法访问 EffectsMenu
。当我尝试它时,它抱怨它在当前上下文中无法访问:
public MainWindow ( )
{
this.InitializeComponent ( );
Console.WriteLine ( EffectsMenu );
}
我也尝试过:
x:Name="EffectsMenu"
但结果相同。
有什么想法可能是错误的以及如何解决它吗?
<ContextMenu x:Key="EffectsContext"
Name="EffectsMenu"
StaysOpen="true">
<MenuItem Header="Add Blur">
<MenuItem.Icon>
<Image Width="16"
Height="16"
Source="{Binding Source={x:Static prop:Resources.BlurIcon},
Converter={StaticResource BitmapToImageSourceConverter}}" />
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="Add Fractal">
<MenuItem.Icon>
<Image Width="16"
Height="16"
Source="{Binding Source={x:Static prop:Resources.Fractalcon},
Converter={StaticResource BitmapToImageSourceConverter}}" />
</MenuItem.Icon>
</MenuItem>
</ContextMenu>
EffectsMenu
isn't accessable in my MainWindow.xaml.cs
file. When I try it, it complains that it's not accessible in the current context:
public MainWindow ( )
{
this.InitializeComponent ( );
Console.WriteLine ( EffectsMenu );
}
I also tried:
x:Name="EffectsMenu"
but same result.
Any ideas what might be wrong and how to fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
试试这个
Try this
如果您添加了 x:key="" 我认为您在 ResourceDictionary 中声明了 ContextMenu(如
)。在这种情况下,您无法直接访问它,请尝试以下操作:Xaml:
代码隐藏:
If you added an x:key="" i think that you declared the ContextMenu in a ResourceDictionary(like
<SomeControl.Resources>
). In this case you can't access it directly, try the following:Xaml:
Code-behind:
删除
ContextMenu
上的x:Key
Remove the
x:Key
on theContextMenu
x:Key 应该是 x:Name 吗?
should that x:Key be x:Name instead?
无法在这里测试,只是猜测:
Can't test here, just a guess: