如何在WPF中正确绑定数据上下文RelativeSource?

发布于 2024-10-08 16:28:11 字数 1470 浏览 7 评论 0 原文

我在 XAML 的模板中定义relativesource,但

DataContext="{Binding RelativeSource={RelativeSource Self}}"

出现异常

“类型的第一次机会异常 “System.Windows.Markup.XamlParseException”发生在 演示框架.dll

附加信息:提供价值 “System.Windows.Markup.StaticResourceHolder”引发异常。”

我认为问题是我需要在 Window.Resources 声明之后绑定它,但我不确定如何使用 标签并仍然使用RelativeSource 谢谢!

<Window x:Class="SupportDesk.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Top Echelon Support Desk" Height="554" Width="743" xmlns:my="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit" xmlns:myNewNamespace="clr-namespace:SupportDesk"
    DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Window.Resources>
    <myNewNamespace:BoolToVisibilityConverter x:Key="boolToVis" />

    <Style TargetType="{x:Type TextBlock}"
  x:Key="GridBlockStyle">
        <Setter Property="VerticalAlignment" Value="Center" />
        <Setter Property="Visibility"
 Value="{Binding Path=IsSelected,
     RelativeSource={RelativeSource FindAncestor,
         AncestorType={x:Type ListViewItem}},
     Converter={StaticResource boolToVis},
         ConverterParameter=False}" />
    </Style>
</Window.Resources>

I am defining the RelativeSource in my Template in the XAML, with

DataContext="{Binding RelativeSource={RelativeSource Self}}"

I am getting an exception

"A first chance exception of type
'System.Windows.Markup.XamlParseException' occurred in
PresentationFramework.dll

Additional information: Provide value on
'System.Windows.Markup.StaticResourceHolder' threw an exception."

I think the problem is i need to bind this after my Window.Resources declarations but i am not sure how to do this using the <DataContext tags and still use RelativeSource. Thanks!

<Window x:Class="SupportDesk.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Top Echelon Support Desk" Height="554" Width="743" xmlns:my="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit" xmlns:myNewNamespace="clr-namespace:SupportDesk"
    DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Window.Resources>
    <myNewNamespace:BoolToVisibilityConverter x:Key="boolToVis" />

    <Style TargetType="{x:Type TextBlock}"
  x:Key="GridBlockStyle">
        <Setter Property="VerticalAlignment" Value="Center" />
        <Setter Property="Visibility"
 Value="{Binding Path=IsSelected,
     RelativeSource={RelativeSource FindAncestor,
         AncestorType={x:Type ListViewItem}},
     Converter={StaticResource boolToVis},
         ConverterParameter=False}" />
    </Style>
</Window.Resources>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

冷︶言冷语的世界 2024-10-15 16:28:11

如何在窗口的直接子窗口上设置 DataContext?例如,

<Window>
   <Grid DataContext="{Binding RelativeSource={RelativeSource Self}}">
   </Grid>
</Window>

这对你有用吗?

How about setting the DataContext on the immediate child of your window? e.g.

<Window>
   <Grid DataContext="{Binding RelativeSource={RelativeSource Self}}">
   </Grid>
</Window>

Would this work for you?

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