如何在WPF中正确绑定数据上下文RelativeSource?
我在 XAML 的模板中定义relativesource,但
DataContext="{Binding RelativeSource={RelativeSource Self}}"
出现异常
“类型的第一次机会异常 “System.Windows.Markup.XamlParseException”发生在 演示框架.dll
附加信息:提供价值 “System.Windows.Markup.StaticResourceHolder”引发异常。”
我认为问题是我需要在 Window.Resources 声明之后绑定它,但我不确定如何使用
<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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如何在窗口的直接子窗口上设置 DataContext?例如,
这对你有用吗?
How about setting the DataContext on the immediate child of your window? e.g.
Would this work for you?