在 DataContext 切换后使用 ValueConverter

发布于 2024-10-05 09:10:37 字数 2276 浏览 8 评论 0原文

我想在一个类中尝试一个小型的自定义 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

厌味 2024-10-12 09:10:37

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.

花伊自在美 2024-10-12 09:10:37

只是几个想法:

  1. 您确定没有错过在 中设置绑定路径吗?
  2. 您是否检查了抛出的异常的 StackTrace 以及所有 InnerExceptions,看看是否有一些有趣的东西?

Just several thoughts:

  1. Are you sure you didn't miss setting binding path in <Binding Converter="{StaticResource displayTypeConverter}" />?
  2. Did you check the StackTrace of the thrown exception and all it's InnerExceptions to see, whether there was something interesting?
我不是你的备胎 2024-10-12 09:10:37

使用更简单的解决方案,如 我的其他相关问题
感谢您的意见!

Used an easier solution, as highlighted in my other related question.
Thank you for your input !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文