我在XAML中的模板中定义了RelativeSource,代码如下:
DataContext="{Binding RelativeSource={RelativeSource Self}}"
我遇到了一个异常。
"在PresentationFramework.dll中出现了类型为'System.Windows.Markup.XamlParseException'的第一次机会异常
其他信息:'System.Windows.Markup.StaticResourceHolder'上提供的值引发了异常。"
我认为问题在于我需要在Window.Resources声明之后绑定它,但是我不知道如何使用<DataContext>标记来实现这一点,并仍然使用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>