3

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...

Peter
  • 37,042
  • 39
  • 142
  • 198
  • I am not fully understanding your situation. Can you post a XAML snippet of your Control and some context around where you are doing this message box call? Also maybe a further explanation of what you are ultimately trying to accomplish would help? There might be a much more effective way to do what you ultimately want. – Brad Cunningham Aug 25 '10 at 17:41
  • What is "design time" ? Do you mean when you mouseover it in the IDE, it says "View" ? – Jimmy Hoffa Aug 25 '10 at 17:57
  • edited it to explain a bit more and added some code.. – Peter Aug 25 '10 at 18:23
  • I reproduced your project but have you noticed that already on design-time if you go into the window showing the test control, the window prompts "Test" ? – Ucodia Aug 31 '10 at 16:04
  • yes but if you open the test control in design mode it says view – Peter Aug 31 '10 at 17:46
  • Hi Petoj, Could you please tell why you need the implemented class details during design-time. So that we can have some more idea. – Prince Ashitaka Sep 02 '10 at 17:15
  • well im trying to get the design time data, and for that i need the type.. im making a custom framework for this so i need the type or at least the name of the type.. – Peter Sep 02 '10 at 22:53

4 Answers4

3

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)!

Mike Schenk
  • 1,512
  • 9
  • 21
1

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.

Omer Mor
  • 5,216
  • 2
  • 34
  • 39
  • 1
    well i don't wanna change what cider does i just want to know the type or the name of the type, this does not have to be done by GetType, it could be any other method or property.. – Peter Sep 03 '10 at 06:30
1

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.

Ondrej Tucny
  • 27,626
  • 6
  • 70
  • 90
  • Well i have a list of Views and ViewModels (or their types) now when im in design time i want to set the DataContext to the right type of viewmodel but to be able to do that i need the type so i can compare the types. – Peter Sep 06 '10 at 06:15
  • @Petoj Updated my answer with a reasonable (IMHO) workaround. – Ondrej Tucny Sep 06 '10 at 18:30
  • @Petoj Read your question once again. Now I understand: you want the name of the **descendant** class. As Omer Mor pointed out in his answer, **there's probably no way to get it** — the fact is the visual designers across Microsoft's technologies do instantiate the _base_ class, not the descendant class being actually designed. So, in your case, it might be faster to solve the problem you are trying to solve by knowing the name of the type in a completely different way. – Ondrej Tucny Sep 06 '10 at 22:23
0

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.

JonWillis
  • 3,146
  • 5
  • 32
  • 54
  • In his question, class 'View' was the base class of class 'Test'. Class 'Test' was the partial class defined in the code-behind of Test.xaml. – Mike Schenk Sep 02 '10 at 22:01
  • No problems, worth a shot though. I remember the problems with DesignMode property with VisualStudio been somewhat useless when nesting views. @Mike Schenk, thanks for clearing that up. In rereading the code that did confuse me not seeing test inherit from view. – JonWillis Sep 02 '10 at 22:05
  • Edit: Partial does only work on one class (you cant have one class thats partial to many classes) and partial is only within the same assembly... – Peter Sep 02 '10 at 22:51