如何在WPF中正确绑定DataContext亲戚?
-
12-10-2019 - |
题
我在XAML中定义了我的模板中的亲戚
DataContext="{Binding RelativeSource={RelativeSource Self}}"
我有例外
“第一个机会'system.windows.markup.xamlparseexception'的第一个机会例外发生在presentationframework.dll中
其他信息:在“ system.windows.markup.staticresourceourceholder”上提供价值。
我认为问题是我需要在窗口后绑定它。Resources声明,但我不确定如何使用 <DataContext
标签并仍然使用亲戚。谢谢!
<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>
解决方案
如何在窗口的直接子里设置DataContext?例如
<Window>
<Grid DataContext="{Binding RelativeSource={RelativeSource Self}}">
</Grid>
</Window>
这个适合您吗?
不隶属于 StackOverflow