2015-07-21 48 views
1

有以下XAML代碼綁定到在相對源屬性向我發送到主窗口+ Catel

<catel:UserControl x:Class="SICUAP.Views.CatProducto_CategoriasView" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:catel="http://catel.codeplex.com" 
      xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro" 
        xmlns:Views="clr-namespace:SICUAP.Views"> 

    <!-- Resources --> 
    <UserControl.Resources>   
    </UserControl.Resources> 

    <!-- Content --> 
    <catel:StackGrid> 
     <catel:StackGrid.RowDefinitions> 
      <RowDefinition Height="Auto"></RowDefinition> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="Auto" /> 
     </catel:StackGrid.RowDefinitions> 
     <Views:cmdGlobalesBDView></Views:cmdGlobalesBDView> 
     <Label Content="Catalogo de Categorias de Producto" Style="{StaticResource estiloTituloControl}"> 
     </Label> 
     <DataGrid Margin="0 10 0 0" MaxHeight="200" ItemsSource="{Binding Producto_Categorias}" SelectedItem="{Binding SelectedProducto_Categoria}" AutoGenerateColumns="False" 
         ScrollViewer.VerticalScrollBarVisibility="Auto" CanUserAddRows="False" CanUserResizeColumns="False" AlternatingRowBackground="#D2EDF7"> 
      <DataGrid.Columns> 
       <DataGridTemplateColumn Header="CLAVE" MinWidth="100"> 
        <DataGridTemplateColumn.CellTemplate> 
         <DataTemplate> 
          <StackPanel> 
           <TextBlock Text="{Binding ID_CATEGORIA}"></TextBlock> 
          </StackPanel> 
         </DataTemplate> 
        </DataGridTemplateColumn.CellTemplate> 
       </DataGridTemplateColumn> 
       <DataGridTemplateColumn Header="NOMBRE" MinWidth="200"> 
        <DataGridTemplateColumn.CellTemplate> 
         <DataTemplate> 
          <StackPanel> 
           <TextBlock Text="{Binding NOMBRE}"></TextBlock> 
          </StackPanel> 
         </DataTemplate> 
        </DataGridTemplateColumn.CellTemplate> 
       </DataGridTemplateColumn> 
       <DataGridTemplateColumn Header="DESCRIPCION" MinWidth="300"> 
        <DataGridTemplateColumn.CellTemplate> 
         <DataTemplate> 
          <StackPanel> 
           <TextBlock Text="{Binding DESCR}" HorizontalAlignment="Stretch"></TextBlock> 
          </StackPanel> 
         </DataTemplate> 
        </DataGridTemplateColumn.CellTemplate> 
       </DataGridTemplateColumn> 
      </DataGrid.Columns>    
      <DataGrid.CellStyle> 
       <Style TargetType="{x:Type DataGridCell}"> 
        <Style.Triggers> 
         <Trigger Property="DataGridCell.IsSelected" Value="True"> 
          <Setter Property="Foreground" Value="Black" /> 
         </Trigger> 
        </Style.Triggers> 
       </Style> 
      </DataGrid.CellStyle> 
     </DataGrid> 
     <Views:dataProducto_CategoriasView DataContext="{Binding SelectedProducto_Categoria}" Visibility="{Binding RelativeSource= {RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}},Path=DataContext.VistaDetalleEsVisible, Converter={StaticResource booleanToVisibilityConverter}}" /> 
    </catel:StackGrid> 
</catel:UserControl> 

當我嘗試內部視圖

<Views:dataProducto_CategoriasView DataContext="{Binding SelectedProducto_Categoria}" Visibility="{Binding RelativeSource= {RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}},Path=DataContext.VistaDetalleEsVisible, Converter={StaticResource booleanToVisibilityConverter}}" /> 

的可見性屬性綁定到它會一直髮送給主窗口的datacontext。

父級加載到主窗口內的contentControl中。

爲什麼我無法綁定到父級用戶控件的datacontext?

回答

0

這與this one的問題完全相同。

閱讀文檔,特別是文章UserControl - Under the hood

+0

感謝您的工作。這個伎倆。 '<網格名稱= 「VistaDetalle」 能見度= 「{結合VistaDetalleEsVisible,轉換器= {StaticResource的booleanToVisibilityConverter}}」> <查看:dataProducto_CategoriasView的DataContext = 「{結合SelectedProducto_Categoria}」> ' –

+1

而不是綁定到DataContext,您還可以綁定到ViewModel: {Binding RelativeSource = {RelativeSource Mode = FindAncestor,AncestorType = {x:Type UserControl}},Path = ViewModel.VistaDetalleEsVisible,Converter = {StaticResource booleanToVisibilityConverter} } –