Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/10.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# 无法向自定义控件添加内容_C#_Wpf_Wpf Controls_Wpf Core_Wpf Core 3.0 - Fatal编程技术网

C# 无法向自定义控件添加内容

C# 无法向自定义控件添加内容,c#,wpf,wpf-controls,wpf-core,wpf-core-3.0,C#,Wpf,Wpf Controls,Wpf Core,Wpf Core 3.0,我正在尝试创建一个自定义版本的UserControl,以便在加载时为视图实现一些标准动画 但是当我添加ContentPresenter时,我无法将内容添加到控件中。为什么呢 这是我用于自定义控件的模板 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/wi

我正在尝试创建一个自定义版本的
UserControl
,以便在加载时为视图实现一些标准动画

但是当我添加
ContentPresenter
时,我无法将内容添加到控件中。为什么呢

这是我用于自定义控件的模板

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:EzNintendo.Desktop.Controls">

    <Style TargetType="{x:Type local:AnimatedView}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:AnimatedView}">
                    <ContentPresenter />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</ResourceDictionary>

这就是我尝试使用它的方式

<controls:AnimatedView x:Class="MyView"
                       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                       xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                       xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                       xmlns:controls="clr-namespace:MControls;assembly=MControls">
    <Grid>
        <TextBlock Text="Hello World!" />
    </Grid>
</controls:AnimatedView>


当我移除
网格时
工作正常

控件类没有内容。尝试从ContentControl派生动画视图,或从
UserControl
派生动画视图,并覆盖XAML中的
ControlTemplate
,在需要的地方插入
ContentPresenter
。@Byoungsammonon您能否将此作为答案发布,以便我可以接受?也许你可以添加更多细节。更多细节?只需将控件修改为ContentControl。或者@MarkFeldman解决方案也不错。
<controls:AnimatedView x:Class="MyView"
                       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                       xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                       xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                       xmlns:controls="clr-namespace:MControls;assembly=MControls">
    <Grid>
        <TextBlock Text="Hello World!" />
    </Grid>
</controls:AnimatedView>