ContentControl 内的 WPF 访问控制
我很难尝试从后面的代码从 ContentControl 内部访问我的网格(listBoxGrid)。任何帮助或指示都会很棒!
谢谢 瑞安
<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);
但这似乎不起作用。
I am having a very hard time trying to access my Grid (listBoxGrid) from inside the ContentControl from code behind. Any help or pointers would be great!
Thanks
Ryan
<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>
I have tried
Grid myGrid = (Grid)Template.FindName("listBoxGrid", this);
But that doesn't seem to work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只能使用 GetTemplateChild("PartName") 在 OnApplyTemplate() 方法重写中访问模板部件。
You can only access template parts in the OnApplyTemplate() method override using GetTemplateChild("PartName").