InvalidOperationException - 无法在当前元素的模板中设置“模板”属性

问题描述 投票:0回答:1

我试图用我的自定义覆盖ListviewItem中的默认ListView,所以我使用了GetContainerForItemOverride()。它按预期工作,但当我尝试覆盖默认的template时,它抛出以下异常:

InvalidOperationException:''Template'属性不能在当前元素的Template中设置'

我错过了什么吗?

<Style TargetType="{x:Type local:StockViewItem}">
        <Setter Property="Background" Value="{x:Null}" />
        <Setter Property="BorderThickness" Value="0" />
        <Setter Property="MinHeight" Value="60px" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:StockViewItem}">
                    <Grid Background="Transparent">
                        <Border
                            x:Name="ContentHost"
                            Margin="0"
                            Padding="{TemplateBinding Padding}"
                            Background="{TemplateBinding DefaultBackground}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            CornerRadius="{TemplateBinding CornerRadius}">
                            <Grid>
                                <GridViewRowPresenter x:Name="gridrowPresenter" Content="{TemplateBinding Property=ContentControl.Content}" />
                                <ContentPresenter
                                    x:Name="contentPresenter"
                                    Content="{TemplateBinding Property=ContentControl.Content}"
                                    Visibility="Collapsed" />
                            </Grid>
                        </Border>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="GridView.ColumnCollection" Value="{x:Null}">
                            <Setter TargetName="contentPresenter" Property="Visibility" Value="Visible" />
                        </Trigger>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter TargetName="ContentHost" Property="Background" Value="{Binding RelativeSource={RelativeSource AncestorType=local:StockViewItem}, Path=ActiveBackground, Mode=TwoWay}" />
                            <Setter Property="Template" Value="{StaticResource SelectedTemplate}" />
                        </Trigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsMouseOver" Value="True" />
                            </MultiTrigger.Conditions>
                            <Setter Property="Cursor" Value="Hand" />
                        </MultiTrigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsSelected" Value="False" />
                                <Condition Property="IsMouseOver" Value="True" />
                            </MultiTrigger.Conditions>
                            <Setter TargetName="ContentHost" Property="Background" Value="{Binding RelativeSource={RelativeSource AncestorType=local:StockViewItem}, Path=HoverBackground, Mode=TwoWay}" />
                        </MultiTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
c# wpf listview listviewitem
1个回答
0
投票

正如Clemens所说,我在Template本身的触发器中使用了Template二传手,而不是Style触发器。

© www.soinside.com 2019 - 2024. All rights reserved.