0
我有一個非常困難的時間試圖從後面的代碼從ContentControl內部訪問我的網格(listBoxGrid)。任何幫助或指針都會很棒!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);
但是,這似乎並沒有工作。
在OnApplyTemplate重寫中,我嘗試使用Grid myGrid = GetTemplateChild(「listBoxGrid」)作爲Grid;仍然myGrid爲空。 – Ryan 2010-12-13 20:58:03
其實只是發現我寫的嚴格適用於Silverlight,在WPF中你應該可以使用Template.FindName(),所以你的問題在別處。您是否嘗試在ContentControl的擴展類中調用該方法? – dain 2010-12-13 22:01:12
看看我的小例子,看看我的問題。如果你喜歡:) http://www.vbninja.com/XAMLGridError.zip – Ryan 2010-12-13 22:22:29