我有一个TabItem,它的DataContext设置为ViewModel。 标题设置为标题视图,内容设置为项目视图
然而,从项目视图中这个绑定不起作用
<TextBox Margin="0" Grid.Column="0" Grid.Row="0" Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=sdk:TabItem, AncestorLevel=1}, Path=DataContext.Job.SourceCode, Mode=TwoWay}"/>
还有这个绑定
<TextBlock Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=sdk:TabItem, AncestorLevel=1}, Path=DataContext.Job.Name}"/>
标题视图中的工作正常。
我甚至尝试将Header中的TextBlock放入Item中,并将Content设置为新的HeaderView。无论我尝试什么,即使标题绑定工作正常,我也无法从内容中获取绑定
任何人都可以解释一下吗?
首先按要求添加完整的视图
<UserControl x:Class="IE.Intergration.JobBuilder.Views.TabbedJobs.TabbedJobItem"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400"
DataContext="{Binding Source={StaticResource ViewModelLocator}, Path=New.TabbedJobs.TabbedJobItem}"
>
<UserControl.Resources>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" >
<Grid.ColumnDefinitions>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="40"/>
</Grid.RowDefinitions>
<TextBox Margin="0" Grid.Column="0" Grid.Row="0" Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=sdk:TabItem, AncestorLevel=1}, Path=DataContext.Job.SourceCode, Mode=TwoWay}"/>
<StackPanel Grid.Column="0" Grid.Row="1" FlowDirection="LeftToRight" Orientation="Horizontal">
<Button Content="Save" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=sdk:TabItem, AncestorLevel=1}, Path=DataContext.Save}" />
</StackPanel>
</Grid>
和标题
<UserControl x:Class="IE.Intergration.JobBuilder.Views.TabbedJobs.TabbedJobItemHeader"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400"
DataContext="{Binding Source={StaticResource ViewModelLocator}, Path=New.TabbedJobs.TabJobHeader}"
>
<Grid x:Name="LayoutRoot">
<TextBlock Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=sdk:TabItem, AncestorLevel=1}, Path=DataContext.Job.Name}"/>
</Grid>