Reputation: 38495
Well i have a control that inherits usercontrol(view) and im using it as you use a usercontrol (a base control) now here is the problem if i do
MessageBox.Show(this.GetType().ToString());
i get different messages in runtime and design time, in design time i get View and i runtime i get the class name of the xaml file inheriting the view...
How can i get the inheriting class type in design time instead of the base class?
Here comes some code:
First we have the view Class
public class View : UserControl
{
public override void OnApplyTemplate()
{
MessageBox.Show(this.GetType().ToString());
base.OnApplyTemplate();
}
}
Then we have a XAML file:
<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>
now if you compile and open "WpfApplication2.Test" in VisualStudio 2010 you will get a message box that says "WpfApplication2.View"..
But if you place the Test control in your MainWindow and press Run(F5) you get WpfApplication2.Test.. what i want is to have the same response in design time that i have in run time...
Upvotes: 3
Views: 1810
Reputation: 27974
Petoj, I think you should ask yourself / describe why you want to know the name of the type and why it's causing troubles when it differs on design time. Unless you're fighting windmills and won't get a reasonable answer in my opinion.
Update — pseudocode of a simple workaround:
if (IsDesignTime)
use this.GetType()
else
use this.GetType().BaseType
Update 2: On design-time, there is no way to get the name of the descendant class being designed. The problem should be probably solved in a different way, not depending on the name of the actual class.
Upvotes: 1
Reputation: 5216
The VS2010 Designer (Cider) is instantiating an instance of the base class when you design a derived control. There's nothing you can do about it.
Upvotes: 1
Reputation: 1582
Well, the problem is that the XAML designer in Visual Studio 2010 does not instantiate the actual class declared in the code-behind. Instead, it only instantiates its base class.
If you think about it, as you modify your XAML, you are actually modifying the very class declared in the code-behind since it is a partial class combined with another part created from the XAML. So the designer can't create an instance of your class: it's still being created.
I don't think you're going to be able to accomplish what you're after without writing code that somehow interacts with Visual Studio itself to ask what file is actually being designed.
You can at least guard your code using a check for DesignerProperties.GetIsInDesignMode().
See these links for some related information:
Troubleshooting WPF Designer load failures
What gets called when the VS 2008 XAML Designer view tries to render the GUI?
Don't do that in the WPF Designer (Cider)!
Upvotes: 3
Reputation: 3147
Im still learning WPF so this probably isn't what you are looking for.
In design time and runtime, this.GetType().ToString(); returns to me "WpfApplication2.View" in the message box.
So view is being returned in both modes. I will state I have made one slight change to your code.
namespace WpfApplication2
{
/// <summary>
/// Interaction logic for View.xaml
/// </summary>
public partial class View : UserControl
{
public override void OnApplyTemplate()
{
MessageBox.Show(this.GetType().ToString());
base.OnApplyTemplate();
}
}
}
I have it marked as a partial class instead of just class, as the XAML is obviously split out from the .cs file. I would not think this would be the problem though.
Upvotes: 0