2010-12-13 2 views
0

Мне очень тяжело пытаться получить доступ к моей Grid (listBoxGrid) изнутри ContentControl из кода позади. Любая помощь или указатели были бы замечательными!WPF Контроль доступа внутри ContentControl

Благодаря Райан

<Style x:Key="ccTemplate" TargetType="{x:Type ContentControl}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type ContentControl}"> 
       <Grid x:Name="listBoxGrid" TextOptions.TextFormattingMode="Display"> 
        <Grid.RowDefinitions> 
         <RowDefinition Height="200*"/> 
         <RowDefinition Height="60" /> 
        </Grid.RowDefinitions> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="80" /> 
         <ColumnDefinition Width="80" /> 
         <ColumnDefinition Width="80" /> 
         <ColumnDefinition Width="80" /> 
         <ColumnDefinition/> 
        </Grid.ColumnDefinitions> 
        <Grid Grid.ColumnSpan="6" > 
         <ContentPresenter /> 
        </Grid> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

<Style x:Key="{x:Type control:ListBoxControl}" TargetType="{x:Type control:ListBoxControl}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate> 
       <ContentControl Style="{StaticResource ccTemplate}" x:Name="cc"> 
        <telerikNavigation:RadTreeView 
    Name="mainList" 
    ItemsSource="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ItemsSource}" 
          ItemTemplate="{DynamicResource MainDataTemplate}" 
          ItemContainerStyle="{DynamicResource MainStyle}" 
    MinWidth="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=MinWidth}" 
    ScrollViewer.HorizontalScrollBarVisibility="Auto" 
    BorderBrush="#FF00569F" 
          Background="{DynamicResource ListItemBackground}" 
    BorderThickness="1" 
          SelectionMode="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=SelectionMode, FallbackValue=Single}" 
          IsDragDropEnabled="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsDragDropEnabled, FallbackValue=False}" 
          IsDropPreviewLineEnabled="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsDropPreviewLineEnabled, FallbackValue=True}" 
          AllowDrop="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=AllowDrop, FallbackValue=True}" 
    > 
        </telerikNavigation:RadTreeView> 
       </ContentControl> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

Я попытался

Grid myGrid = (Grid)Template.FindName("listBoxGrid", this); 

Но это не похоже на работу.

ответ

2

Доступ к частям шаблона возможен только при переопределении метода OnApplyTemplate() с использованием GetTemplateChild ("PartName").

+0

В переопределении OnApplyTemplate я попробовал Grid myGrid = GetTemplateChild ("listBoxGrid") как Grid; и все еще myGrid имеет значение null. – Ryan

+0

На самом деле только что выяснилось, что то, что я написал, строго относится только к Silverlight, в WPF вы должны иметь возможность использовать Template.FindName(), поэтому ваша проблема находится в другом месте. Вы пытались вызвать метод в классе расширения ContentControl? – dain

+0

Проверьте мой маленький пример, чтобы увидеть мою проблему. Если вам нравится :) http://www.vbninja.com/XAMLGridError.zip – Ryan