0

我想通过相对源将我的 DataTemplate (ClosableTabItemTemplate) 绑定到 HeaderedContentControl 的 Workspaces DataContext。不幸的是它不起作用?有任何想法吗?

顺便说一句:我收到以下命令行错误:System.Windows.Data 错误:40:BindingExpression 路径错误:在“对象”“AllUserView”(名称=“”)上找不到“DisplayName”属性。BindingExpression:Path=DataContext.DisplayName; DataItem='TabItem' (名称=''); 目标元素是'ContentPresenter'(名称='');目标属性是“内容”(类型“对象”)

赛亚迈克尔

<HeaderedContentControl 
      Content="{Binding Path=Workspaces}"
      ContentTemplate="{StaticResource WorkspacesTemplate}"
      Header=""
      Style="{StaticResource MainHCCStyle}" DataContext="{Binding}" 
                />
<DataTemplate x:Key="ClosableTabItemTemplate">
    <DockPanel Width="120"  >
        <Button 
    Command="{Binding Path=DataContext.CloseCommand, RelativeSource={RelativeSource AncestorType={x:Type HeaderedContentControl}}}"
    Content="X"
    Cursor="Hand"
    DockPanel.Dock="Right"
    Focusable="False"
    FontFamily="Courier" 
    FontSize="9"
    FontWeight="Bold"  
    Margin="0,1,0,0"
    Padding="0"
    VerticalContentAlignment="Bottom"
    Width="16" Height="16" 
    />
    <ContentPresenter 
    Content="{Binding Path=DataContext.DisplayName, RelativeSource={RelativeSource AncestorType={x:Type HeaderedContentControl}}}" 
    VerticalAlignment="Center" 
    />
    </DockPanel>
</DataTemplate>

<DataTemplate x:Key="WorkspacesTemplate">
    <TabControl 
  IsSynchronizedWithCurrentItem="True" 
  ItemsSource="{Binding}"      
  ItemTemplate="{StaticResource ClosableTabItemTemplate}"
  Margin="4"
  />
</DataTemplate>
4

1 回答 1

0

我认为您必须使用RelativeSource.AncestorLevel Property。默认级别是 1,您的 WorkspacesTemplateTabControl也是HeaderedContentControl。尝试

Content="{Binding Path=DataContext.DisplayName,
                  RelativeSource={RelativeSource AncestorType={x:Type HeaderedContentControl},
                                                 AncestorLevel=2}}" 

找到你的真实HeaderedContentControl

于 2012-07-21T16:52:24.127 回答