Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.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
Wpf 尝试在DataTemplate内使用ContentPresenter时,UserControl内容为空_Wpf_Inheritance_User Controls_Contentpresenter - Fatal编程技术网

Wpf 尝试在DataTemplate内使用ContentPresenter时,UserControl内容为空

Wpf 尝试在DataTemplate内使用ContentPresenter时,UserControl内容为空,wpf,inheritance,user-controls,contentpresenter,Wpf,Inheritance,User Controls,Contentpresenter,我有一个主要的抽象代码类BaseImportorButton,继承UserControl。我有一个子类ClickableImageButton,带有xaml和代码隐藏 我在ControlTemplate中使用以下样式: <Style TargetType="{x:Type local:ClickableImageButton}"> <Setter Property="ToolTipService.InitialShowDelay" Value="0"/>

我有一个主要的抽象代码类BaseImportorButton,继承UserControl。我有一个子类ClickableImageButton,带有xaml和代码隐藏

我在ControlTemplate中使用以下样式:

<Style TargetType="{x:Type local:ClickableImageButton}">
    <Setter Property="ToolTipService.InitialShowDelay" Value="0"/>
    <Setter Property="BorderThickness" Value="0"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:ClickableImageButton}">
                <StackPanel Margin="{TemplateBinding Margin}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" VerticalAlignment="{TemplateBinding VerticalAlignment}">
                    <ContentPresenter
                        Content="{TemplateBinding Content}"
                        ContentTemplate="{TemplateBinding ContentTemplate}" />
                </StackPanel>
            </ControlTemplate>                
        </Setter.Value>
    </Setter>
</Style>

在ListView中用作原始ListViewItem时,我的ClickableImageButton会正确显示

但是:当在带有ItemTemplate DataTemplate的listview中使用它时,不再显示ClickableImageButton。。。就好像在DataTemplate中内容是空的一样

我找到的解决方案是在BaseImportorButton上编写DependencyProperty ButtonContent,并在xaml中显式设置它

但是有人能解释这个问题吗

编辑:下面是两个不同的xaml 正确显示基础图像的图像(ClickableImage是一个图像)


还有那个不起作用的

<ListView Grid.Row="1" Name="ListViewSections" ItemsSource="{Binding Path=Sections}" Background="{x:Null}" SizeChanged="ListViewSections_SizeChanged">                    
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Grid MaxWidth="600">
                        <Grid.RowDefinitions>
                            <RowDefinition/>
                            <RowDefinition/>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition MaxWidth="150"/>
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>
                        <TextBlock Grid.Row="0" Grid.ColumnSpan="2" Text="{Binding Path=Titre}" Margin="5,0,5,0" FontSize="18" FontWeight="Bold" Foreground="White" TextWrapping="Wrap" FontFamily="Arial" HorizontalAlignment="Left" />
                        <local:ClickableImageButton Grid.Row="1" Tag="{Binding Path=Id}" Grid.Column="0" ImpostorClick="Image_Click" Margin="10">
                            <local:ClickableImageButton.Content>
                                <local:ClickableImage Source="Content/tada.png" />
                            </local:ClickableImageButton.Content>
                        </local:ClickableImageButton>
                        <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Path=Texte}" Margin="10,0,10,0" FontSize="16" Foreground="White" TextWrapping="Wrap" FontFamily="Arial" VerticalAlignment="Center" HorizontalAlignment="Left" />
                    </Grid>
                </DataTemplate>                    
            </ListView.ItemTemplate>                
        </ListView>

所以我终于让它开始工作了。这并不是因为我是从UserControl派生出来的

所以我一直从UserControl派生BaseImportorButton,我清空了ControlTemplate中的ContentPresenter(

我不必设置DefaultStyleKey

我只需要从子类xaml文件中删除任何内容。事实上,当我创建我的子用户控件时,我没有删除添加的默认内容,即
。去掉这个就解决了我所有的问题

看起来,当我们尝试从模板化上下文设置默认内容时,默认内容不会被覆盖,而仅当从非模板化上下文设置时才会被覆盖

ie前代码:

<sk:BaseImpostorButton x:Class="Compteur_3D.ClickableImageButton"
           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"
  mc:Ignorable="d"
         d:DesignHeight="300" d:DesignWidth="300">
<Grid/>
</sk:BaseImpostorButton>

修订守则:

<sk:BaseImpostorButton x:Class="Compteur_3D.ClickableImageButton"
         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"              mc:Ignorable="d"
         d:DesignHeight="300" d:DesignWidth="300">
</sk:BaseImpostorButton>

编辑:在运行时,删除
时,我的ClickableImageButton元素具有正确的内容(即ClickableImage)。但是当
未删除时,我的ClickableImageButton将空网格作为内容。。。好像在以下位置设置内容时未覆盖此

<local:ClickableImageButton ImpostorClick="Image_Click" Margin="10">
                            <local:ClickableImageButton.Content>
                                <local:ClickableImage Source="{Binding Path=Image.Source}" />
                            </local:ClickableImageButton.Content>
                        </local:ClickableImageButton>


编辑:目标框架4客户端配置文件

如果不使用用户控制,为什么要从中派生?我建议直接从ContentControl派生。控件模板中的ContentPresenter会自动为内容、ContentTemplate和ContentTemplateSelector获取模板绑定。。。从ContentControl派生基本抽象类不会改变任何东西。UserControl是一个ContentControl您是否在数据模板中编写了
?不完全是:
ClickableImage是我从XML数据构造的一个纯代码类。此代码在ListViewItem的范围内工作,但在ItemTemplate/DataTemplate的范围内不工作
<local:ClickableImageButton ImpostorClick="Image_Click" Margin="10">
                            <local:ClickableImageButton.Content>
                                <local:ClickableImage Source="{Binding Path=Image.Source}" />
                            </local:ClickableImageButton.Content>
                        </local:ClickableImageButton>