好吧,我有一个继承用户控件(视图)的控件,并且我在使用用户控件(基本控件)时使用它,如果我这样做,这就是问题所在
MessageBox.Show(this.GetType().ToString());
我在运行时和设计时收到不同的消息,在设计时我得到视图,我在运行时得到继承视图的 xaml 文件的类名......
如何在设计时获取继承类类型而不是基类?
这里有一些代码:
首先我们有视图类
public class View : UserControl
{
public override void OnApplyTemplate()
{
MessageBox.Show(this.GetType().ToString());
base.OnApplyTemplate();
}
}
然后我们有一个 XAML 文件:
<local:View x:Class="WpfApplication2.Test"
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"
xmlns:local="clr-namespace:WpfApplication2"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
</Grid>
</local:View>
现在,如果您在 VisualStudio 2010 中编译并打开“WpfApplication2.Test”,您将收到一个显示“WpfApplication2.View”的消息框。
但是,如果您将测试控件放在 MainWindow 中并按 Run(F5),您将得到 WpfApplication2.Test.. 我想要的是在设计时具有与运行时相同的响应...