ContentControl 内的 WPF 访问控制

发布于 2024-10-07 17:45:18 字数 3116 浏览 0 评论 0原文

我很难尝试从后面的代码从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼眸印温柔 2024-10-14 17:45:18

您只能使用 GetTemplateChild("PartName") 在 OnApplyTemplate() 方法重写中访问模板部件。

You can only access template parts in the OnApplyTemplate() method override using GetTemplateChild("PartName").

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文