在 DataContext 切换后使用 ValueConverter
我想在一个类中尝试一个小型的自定义 ValueConverter
,该类在实例化后(通过默认构造函数,仅调用 InitializeComponents()
),给出另一个 DataContext,特别是 ViewModel 的实例。
在 Binding
中使用 StaticResource
根本不起作用(产生 NullReferenceException
),因为 DataContext
已经然后被更改(不再是this
)。
我尝试将 DataContext = this;
放在 InitializeComponents
调用之前,没有任何变化。 我是否应该研究这个 MarkupExtension
gizmo(如在此描述的)文章) ?
我还尝试在 ViewModel(当前的 DataContext)中创建自定义的 Value Converter 实例,但也没有帮助。
我可以随时提供更多详细信息。先感谢您 !
我正在尝试在 TextBlock 中显示上下文菜单。 ContextMenu 包含一个唯一的 MenuItem。例如,菜单项的标题可以是“设置”。所述 MenuItem 的子项(也呈现为 MenuItems)源自枚举,因此是 MenuItem 上的 ItemsSource。
现在一切都显示得很好,但我试图默认选择其中一个子项(例如枚举的成员),因为已经有一个默认设置。更多背景信息可以在 我的其他问题。
编辑:
...
<UserControl.Resources>
<Helpers:DisplayTypeToDefaultValueConverter x:Key="displayTypeConverter" />
</UserControl.Resources>
...
<TextBlock x:Name="InstructionLabel"
TextWrapping="Wrap" Text="{Binding Path=SelectedNodeText}"
Grid.RowSpan="1">
<TextBlock.ContextMenu>
<ContextMenu>
<MenuItem Header="Settings" Name="SettingsPop"
DataContext="{Binding}"
ItemsSource="{Binding Source={StaticResource DisplayTypeValues}}"
IsCheckable="True"
Click="SettingsType_Click">
<MenuItem.ItemContainerStyle>
<Style TargetType="MenuItem">
<Setter Property="Header" Value="{Binding}"/>
<Setter Property="IsChecked">
<Setter.Value>
<Binding Converter="{StaticResource displayTypeConverter}" />
</Setter.Value>
</Setter>
</Style>
</MenuItem.ItemContainerStyle>
</ContextMenu>
</TextBlock>
我现在才意识到这是可怕的ContextMenu。这就是问题所在,不是吗?
I want to try out a small, custom ValueConverter
in a class which after being instantiated (via default constructor, which only calls InitializeComponents()
), is given another DataContext
, specifically an instance of a ViewModel.
Using a StaticResource
within the Binding
does not work at all (yields a NullReferenceException
), since the DataContext
has since then been changed (is not this
anymore).
I've tried putting DataContext = this;
before the InitializeComponents
call, no change.
Should I be looking into this MarkupExtension
gizmo (as described in this article) ?
I've also tried creating an instance of the custom Value Converter
within the ViewModel (the current DataContext
), doesn't help either.
I can provide additional details at all times. Thank you in advance !
I'm trying to display a ContextMenu within the TextBlock. The ContextMenu contains a sole MenuItem. The header of the MenuItem can be "Settings" for instance. The Children (rendered as MenuItems as well) of the said MenuItem stem from an Enum, hence the ItemsSource on the MenuItem.
Now all is getting displayed nicely, yet I am trying to make one of the Children (e.g. a member of the Enum) to be selected per default, since there is already a default Setting. Further background info can be found in my other question.
Edit :
...
<UserControl.Resources>
<Helpers:DisplayTypeToDefaultValueConverter x:Key="displayTypeConverter" />
</UserControl.Resources>
...
<TextBlock x:Name="InstructionLabel"
TextWrapping="Wrap" Text="{Binding Path=SelectedNodeText}"
Grid.RowSpan="1">
<TextBlock.ContextMenu>
<ContextMenu>
<MenuItem Header="Settings" Name="SettingsPop"
DataContext="{Binding}"
ItemsSource="{Binding Source={StaticResource DisplayTypeValues}}"
IsCheckable="True"
Click="SettingsType_Click">
<MenuItem.ItemContainerStyle>
<Style TargetType="MenuItem">
<Setter Property="Header" Value="{Binding}"/>
<Setter Property="IsChecked">
<Setter.Value>
<Binding Converter="{StaticResource displayTypeConverter}" />
</Setter.Value>
</Setter>
</Style>
</MenuItem.ItemContainerStyle>
</ContextMenu>
</TextBlock>
I've only now realized that it's the dreaded ContextMenu. That's the problem, isn't it ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ItemContainerStyle 内的DataContext 是DisplayTypeValues 集合的成员。在您发布的 XAML 中,唯一会受到 UserControl 的 DataContext 更改影响的是指令标签的文本。像在 MenuItem 上所做的那样设置 DataContext="{Binding}" 也是多余的,因为该值已经从父 ContextMenu 继承。
从您的问题或代码中不清楚您对 DataContext 的期望或您试图用它做什么。
The DataContext inside the ItemContainerStyle is a member of the DisplayTypeValues collection. The only thing in the XAML you posted that will be affected by the DataContext of the UserControl changing is the InstructionLabel's text. Setting DataContext="{Binding}" as you're doing on the MenuItem is also redundant as the value will already be inherited from the parent ContextMenu.
It's not clear from your question or code what you're expecting from the DataContext or what you're trying to do with it.
只是几个想法:
中设置绑定路径吗?Just several thoughts:
<Binding Converter="{StaticResource displayTypeConverter}" />
?使用更简单的解决方案,如 我的其他相关问题。
感谢您的意见!
Used an easier solution, as highlighted in my other related question.
Thank you for your input !