首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

WPF,这段代码为何报错,却能运行

2013-09-15 
WPF,这段代码为什么报错,却能运行?Window.ResourcesControlTemplate x:KeyResizeDecoratorTemplate

WPF,这段代码为什么报错,却能运行?


<Window.Resources>
    <ControlTemplate x:Key="ResizeDecoratorTemplate" TargetType="Control">
        <Grid>
            <local:ResizeThumb Height="3" Cursor="SizeNS" Margin="0 -4 0 0"
        VerticalAlignment="Top" HorizontalAlignment="Stretch"/>
            <local:ResizeThumb Width="3" Cursor="SizeWE" Margin="-4 0 0 0"
        VerticalAlignment="Stretch" HorizontalAlignment="Left"/>
            <local:ResizeThumb Width="3" Cursor="SizeWE" Margin="0 0 -4 0"
        VerticalAlignment="Stretch" HorizontalAlignment="Right"/>
            <local:ResizeThumb Height="3" Cursor="SizeNS" Margin="0 0 0 -4"
        VerticalAlignment="Bottom"  HorizontalAlignment="Stretch"/>
            <local:ResizeThumb Width="7" Height="7" Cursor="SizeNWSE" Margin="-6 -6 0 0"
        VerticalAlignment="Top" HorizontalAlignment="Left"/>
            <local:ResizeThumb Width="7" Height="7" Cursor="SizeNESW" Margin="0 -6 -6 0"
        VerticalAlignment="Top" HorizontalAlignment="Right"/>
            <local:ResizeThumb Width="7" Height="7" Cursor="SizeNESW" Margin="-6 0 0 -6"
        VerticalAlignment="Bottom" HorizontalAlignment="Left"/>
            <local:ResizeThumb Width="7" Height="7" Cursor="SizeNWSE" Margin="0 0 -6 -6"
        VerticalAlignment="Bottom" HorizontalAlignment="Right"/>
        </Grid>
    </ControlTemplate>
    <Style TargetType="Button">
        <Setter Property="Template" >
            <Setter.Value>


                <ControlTemplate TargetType="Button">
                    <Grid DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}">
                        <Control Template="{StaticResource ResizeDecoratorTemplate}" />
                        <ContentPresenter Content="{TemplateBinding ContentControl.Content}"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Grid HorizontalAlignment="Left" Height="485" VerticalAlignment="Top" Width="766">
    <Button Content="Button" HorizontalAlignment="Left" Margin="158,184,0,0" VerticalAlignment="Top" Width="75"/>        
</Grid>




class ResizeThumb : Thumb
{
   ...
   ...
}

此段代码,在模板中的"<Control Template="{StaticResource ResizeDecoratorTemplate}" />"处报错:未将对象引用设置到对象的实例
怎么会出现这种异常,如果把Button删除,异常就消失了,或者把ResizeDecoratorTemplate模板中的那些Thumb删除,异常也就消失了。我想是不是因为继承自Thumb自定义类引起的?

[解决办法]
自定义类型要编译一次后就不会报错了,另外记得要将class定义为public。
[解决办法]
ResizeThumb?可能不正常.

热点排行