WPF get development time type?

Well, I have a control that inherits usercontrol (view) and im using it since you are using usercontrol (the base control), now the problem is if I do

MessageBox.Show(this.GetType().ToString()); 

I get different messages at runtime and development time, during development I get View and I runtime. I get the class name of the xaml file inheriting the view ...

How can I get the type of an inherited class at design time instead of the base class?

Here is the code:

First we have the class Class

 public class View : UserControl { public override void OnApplyTemplate() { MessageBox.Show(this.GetType().ToString()); base.OnApplyTemplate(); } } 

Then we have the 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 window with the message "WpfApplication2.View" ..

But if you put the control in your MainWindow and press Run (F5), you get WpfApplication2.Test .. what I want should have the same answer at design time that I have at runtime ...

+3
c # xaml design-time
source share
4 answers

Well, the problem is that the XAML designer in Visual Studio 2010 does not instantiate the actual class declared in the code. Instead, it only creates its base class.

If you think about it, as your XAML changes, you actually change the class itself, declared in the code, as it is a partial class in combination with another part created from XAML. Therefore, the developer cannot create an instance of your class: it is still being created.

I don’t think you can accomplish what you need without writing code that somehow interacts with Visual Studio itself to ask which file is actually being developed.

You can at least protect your code using validation for DesignerProperties.GetIsInDesignMode ().

See these links for some related information:

Troubleshooting WPF Designer Boot Errors

What is called when the VS 2008 XAML Designer view tries to display a graphical interface?

Do not do this in WPF Designer (Cider)!

+3
source share

The VS2010 constructor (Cider) creates an instance of the base class when developing a derived control. There is nothing you can do about it.

+1
source share

Petey, I think you should ask yourself / describe why you want to know the type name and why it causes problems when it differs in development time. If you do not fight with windmills and in my opinion you will not get a reasonable answer.

Update - pseudo-code of a simple workaround:

 if (IsDesignTime) use this.GetType() else use this.GetType().BaseType 

Update 2:. Development time does not allow to get the name of the class being developed a descendant . The problem should probably be solved in a different way, independent of the name of the actual class.

+1
source share

I'm still learning WPF, so this is probably not what you are looking for.

At design time and runtime, this.GetType (). ToString (); returns me "WpfApplication2.View" in the message field.

Thus, the view returns in both modes. I will indicate that I made a small 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 designated it as an incomplete class, not just a class, since XAML is clearly separated from the .cs file. I would not think that this would be a problem.

0
source share

All Articles