在 Visual Studio 2010 中冻结的 WPF 控件 [英] WPF control frozen in Visual Studio 2010

查看:27
本文介绍了在 Visual Studio 2010 中冻结的 WPF 控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 WPF 中做了一个自定义按钮控件,我正在尝试在 WinForms (c#) 中实现它.现在,该控件正在运行并且编译良好,但我似乎无法访问 elementhost - 该控件未显示在工具箱中,之后我看到它被冻结,即我无法将其添加到表单中.由于项目构建过程没有错误或警告,我不知道是什么导致了问题.任何帮助将不胜感激,谢谢.

I did a custom button control in WPF and I am trying to implement it in WinForms (c#). Now, the control is working and it compiles well, but I can't seem to access the elementhost - the control was not shown in the toolbox, and afterwards I saw it's frozen, i.e. I can't add it to the form. Since the project build process went without errors or warnings, I don't know what is causing the problem. Any help would be appreciated, thanks.

这是 xaml 文件的代码:

Here's the code of the xaml file:

<Button x:Class="proba4.UserControl1"
             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="120" d:DesignWidth="300" IsEnabled="True">
    <Button.Template>
        <ControlTemplate TargetType="{x:Type Button}">
            <Grid>
                <Image Name="Normal" Source="C:\stuff\off_button.gif"/>
                <Image Name="Pressed" Source="C:\stuff\on_button.gif" Visibility="Hidden"/>

            </Grid>
            <ControlTemplate.Triggers>
                <Trigger Property="IsPressed" Value="True">
                    <Setter TargetName="Normal" Property="Visibility" Value="Hidden"/>
                    <Setter TargetName="Pressed" Property="Visibility" Value="Visible"/>
                </Trigger>

            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Button.Template>
</Button>

推荐答案

可能是因为你从非用户控件类继承了你的控件?在 WPF 中,第一条规则是避免控件继承.尝试使用组合

May be it is because you inherit your control from non-usercontrol class? In WPF the first rule is avoiding control inheritance. Try to use composition

这篇关于在 Visual Studio 2010 中冻结的 WPF 控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆