Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ContentControl与内容周围的控件_C#_Wpf_Custom Controls - Fatal编程技术网

C# ContentControl与内容周围的控件

C# ContentControl与内容周围的控件,c#,wpf,custom-controls,C#,Wpf,Custom Controls,我想在WPF中创建一个可以包含一个元素的自定义ContentControl。这很容易,我已经做了很多次了。但在这里,我希望内容控件的底部和左侧边缘都有标尺。我希望这些控件可以从代码隐藏中访问。我不知如何前进。我考虑过一个模板,但是标尺控件不容易访问。我还考虑过创建一个带有类似内容的依赖属性的UserControl,但是对于这个控件的用户来说,XAML并不像只使用内容控件那么简单 谢谢。您可以使用模板部件。您可以在此页面上查看完整的示例: 从该示例中,以下是CustomControl如何声明模板

我想在WPF中创建一个可以包含一个元素的自定义
ContentControl
。这很容易,我已经做了很多次了。但在这里,我希望内容控件的底部和左侧边缘都有标尺。我希望这些控件可以从代码隐藏中访问。我不知如何前进。我考虑过一个模板,但是标尺控件不容易访问。我还考虑过创建一个带有类似内容的依赖属性的
UserControl
,但是对于这个控件的用户来说,XAML并不像只使用内容控件那么简单


谢谢。

您可以使用模板部件。您可以在此页面上查看完整的示例:

从该示例中,以下是
CustomControl
如何声明模板部件:

[TemplatePart(Name=“UpButtonElement”,Type=typeof(RepeatButton))]
[...]
公共类NumericUpDown:控件
[…]

以及如何访问它们:

  public override void OnApplyTemplate()
    {
        UpButtonElement = GetTemplateChild("UpButton") as RepeatButton;
        [...]
    }

您所要求的是能够以两种常用的方式使用内容,但同时使用一个控件。您可以这样做,但只需要两个不同的内容属性。您可以定义自己的次要内容属性集,但只从
HeaderContentControl
派生要容易得多,它已经通过其
标题
属性为您做到了这一点

要允许控件的用户将其视为普通的
ContentControl
,只需将内容包含在标记中,您可以使用
标题
内容作为定义
UserControl
样式的部分,并可以从代码隐藏处进行访问。在该内容中,您只需将一个
ContentPresenter
放在您希望接收外部
内容的任何位置即可。然后还需要调整
控制模板
以仅显示
标题
内容(
内容
将位于
标题
部分中)


当你说你想从代码隐藏中访问这些控件时,你能解释一下你的意思吗?我想能够访问自定义ContentControl的C#代码中的标尺控件。
<HeaderedContentControl x:Class="WpfApp.MyRulerControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApp">
    <HeaderedContentControl.Template>
        <ControlTemplate TargetType="{x:Type local:MyRulerControl}">
            <Border Background="{TemplateBinding Background}">
                <ContentPresenter ContentSource="Header"/>
            </Border>
        </ControlTemplate>
    </HeaderedContentControl.Template>

    <HeaderedContentControl.Header>
        <StackPanel>
            <Border x:Name="Ruler1" Height="20"/>

            <ContentPresenter
                Content="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:MyRulerControl}}, Path=Content}"
                ContentTemplate="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:MyRulerControl}}, Path=ContentTemplate}"/>

            <Border x:Name="Ruler2" Height="20"/>
        </StackPanel>
    </HeaderedContentControl.Header>
</HeaderedContentControl>
<local:MyRulerControl>
    <TextBlock Text="External content here"/>
</local:MyRulerControl>
    public MyRulerControl()
    {
        InitializeComponent();

        Ruler1.Background = Brushes.Red;
        Ruler2.Background = Brushes.Blue;
    }