C# 从资源绑定到datacontext
我在usercontrols资源中定义了一个contextmenu,usercontrols datacontext被设置为一个单独的viewmodel,其中包含我想要绑定到的属性C# 从资源绑定到datacontext,c#,wpf,C#,Wpf,我在usercontrols资源中定义了一个contextmenu,usercontrols datacontext被设置为一个单独的viewmodel,其中包含我想要绑定到的属性 <UserControl> <UserControl.Resources> <ResourceDictionary> <ContextMenu> <MenuItem Visiblity="{Binding IsVisible}"/&
<UserControl>
<UserControl.Resources>
<ResourceDictionary>
<ContextMenu>
<MenuItem Visiblity="{Binding IsVisible}"/>
</ContextMenu>
<ResourceDictionary>
<Grid x:Name="MyGrid">
</Grid>
</UserControl>
我怀疑“内容”不包括UserControl.Resources
。我想在我的绑定中需要一些源代码,但是怎么做呢
谢谢 如果
可见
属性位于viewmodel中,则无需使用相对资源
例如:
<Style TargetType="{x:Type local:MyControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:MyControl}">
<StackPanel>
<TextBlock Text="Hello, World!" />
<CheckBox x:Name="MainContentArea" IsChecked="{Binding IsVisible}" />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type local:MyControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:MyControl}">
<StackPanel>
<TextBlock Text="Hello, World!" />
<CheckBox x:Name="MainContentArea" IsChecked="{Binding IsVisible}" />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>