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

时间:2013-01-18 13:41:53

标签: wpf inheritance user-controls contentpresenter

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

我在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中内容为空时一样。

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

但有人可以解释这个问题吗?

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

<ListView Grid.Row="1" Name="ListViewSections" ItemsSource="{Binding Path=Sections}" Background="{x:Null}" SizeChanged="ListViewSections_SizeChanged">                    
            <ListViewItem>
<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>                                       
            </ListViewItem>                
        </ListView>

那个不起作用的那个

<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>

1 个答案:

答案 0 :(得分:0)

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

所以我一直从UserControl派生出BaseImpostorButton,我在ControlTemplate(<ContentPresenter />)中清空了ContentPresenter。

我没有设置DefaultStyleKey。

我只需要从子类xaml文件中删除任何内容。实际上,当我创建我的孩子UserControl时,我没有删除添加的默认内容,即<Grid />。删除它只是解决了我所有的问题。

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

即前代码:

<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元素在删除<Grid />时具有正确的内容(即ClickableImage)。但是,如果未删除<Grid />,则我的ClickableImageButton将空网格作为内容...就像在设置内容时未覆盖此<Grid />一样:

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

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

相关问题