Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > Avalon

Programmatic object creation

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 05-02-2006   #1 (permalink)
Peter
Guest


 

Programmatic object creation

I would like to create WPF objects programmatically. Does anyone know of a
tutorial for this? For example, I would to do something like...

Ellipse myEllipse = new Ellipse();

Unfortunately, I can't get this syntax to compile. Ideally I would be able
to create and define a particular kind of object in XAML/C# and then
instantiate it. It appears like I need to create a resource defintion in XAML
and associate it with my codebehind? I'm not finding examples of this and
haven't been able to hack something together on my own.

I tried the free cell sample, but this implementation seems rather abstruse
to me. Can anyone point me to a more basic example? Thanks.

My System SpecsSystem Spec
Old 05-02-2006   #2 (permalink)
Drew Marsh
Guest


 

Re: Programmatic object creation

peter wrote:

> I would like to create WPF objects programmatically. Does anyone know
> of a tutorial for this? For example, I would to do something like...
>
> Ellipse myEllipse = new Ellipse();
>
> Unfortunately, I can't get this syntax to compile. Ideally I would be
> able to create and define a particular kind of object in XAML/C# and
> then instantiate it. It appears like I need to create a resource
> defintion in XAML and associate it with my codebehind? I'm not finding
> examples of this and haven't been able to hack something together on
> my own.
>
> I tried the free cell sample, but this implementation seems rather
> abstruse to me. Can anyone point me to a more basic example? Thanks.


You should definitely be able to do exactly what you're trying to do. It's
a very straightforward programming model. What erros are you getting?

-Drew
___________________________________
Drew Marsh
Chief Software Architect
Mimeo.com, Inc. - http://www.mimeo.com
Microsoft C# / WPF MVP
Weblog - http://blog.hackedbrain.com/


My System SpecsSystem Spec
Old 05-02-2006   #3 (permalink)
Peter
Guest


 

Re: Programmatic object creation

Compiler says that Ellipse is a 'field' but is used like a 'type'? I do have
an Ellipse in my xaml so I suppose it thinks I'm trying to instantiate an
instance rather than a class definition? But isn't Ellipse a WPF type? How do
I refer to the type?

Thanks!

Peter

"Drew Marsh" wrote:

> peter wrote:
>
> > I would like to create WPF objects programmatically. Does anyone know
> > of a tutorial for this? For example, I would to do something like...
> >
> > Ellipse myEllipse = new Ellipse();
> >
> > Unfortunately, I can't get this syntax to compile. Ideally I would be
> > able to create and define a particular kind of object in XAML/C# and
> > then instantiate it. It appears like I need to create a resource
> > defintion in XAML and associate it with my codebehind? I'm not finding
> > examples of this and haven't been able to hack something together on
> > my own.
> >
> > I tried the free cell sample, but this implementation seems rather
> > abstruse to me. Can anyone point me to a more basic example? Thanks.

>
> You should definitely be able to do exactly what you're trying to do. It's
> a very straightforward programming model. What erros are you getting?
>
> -Drew
> ___________________________________
> Drew Marsh
> Chief Software Architect
> Mimeo.com, Inc. - http://www.mimeo.com
> Microsoft C# / WPF MVP
> Weblog - http://blog.hackedbrain.com/
>
>
>

My System SpecsSystem Spec
Old 05-02-2006   #4 (permalink)
Douglas Stockwell
Guest


 

Re: Programmatic object creation

Do you have <Ellipse Name="Ellipse" /> ?

The names you specify as you have discovered will appear as fields from your
code-behind.

However, that should still work. I'm guessing you are missing a namespace
import.

Do you have "using System.Windows.Shapes;" ?

- Doug

> Compiler says that Ellipse is a 'field' but is used like a 'type'? I
> do have an Ellipse in my xaml so I suppose it thinks I'm trying to
> instantiate an instance rather than a class definition? But isn't
> Ellipse a WPF type? How do I refer to the type?
>
> Thanks!
>
> Peter
>
> "Drew Marsh" wrote:
>
>> peter wrote:
>>
>>> I would like to create WPF objects programmatically. Does anyone
>>> know of a tutorial for this? For example, I would to do something
>>> like...
>>>
>>> Ellipse myEllipse = new Ellipse();
>>>
>>> Unfortunately, I can't get this syntax to compile. Ideally I would
>>> be able to create and define a particular kind of object in XAML/C#
>>> and then instantiate it. It appears like I need to create a resource
>>> defintion in XAML and associate it with my codebehind? I'm not
>>> finding examples of this and haven't been able to hack something
>>> together on my own.
>>>
>>> I tried the free cell sample, but this implementation seems rather
>>> abstruse to me. Can anyone point me to a more basic example? Thanks.
>>>

>> You should definitely be able to do exactly what you're trying to do.
>> It's a very straightforward programming model. What erros are you
>> getting?
>>
>> -Drew
>> ___________________________________
>> Drew Marsh
>> Chief Software Architect
>> Mimeo.com, Inc. - http://www.mimeo.com
>> Microsoft C# / WPF MVP
>> Weblog - http://blog.hackedbrain.com/



My System SpecsSystem Spec
Old 05-03-2006   #5 (permalink)
Peter
Guest


 

Re: Programmatic object creation

Duh... good guess. Thanks.

Still, I'd say there are several things that could be improved here. First
of all, the compiler didn't suggest that I might be missing a using statement
as it usually does and instead gave me the worthless bracket missing error.

Also, it is interesting that I can add an Ellipse in XAML without importing
the library. Does VS do this for you when you add a control from the toolbox
to the designer? I'm not sure. Maybe this this is deliberate?

Peter

"Douglas Stockwell" wrote:

> Do you have <Ellipse Name="Ellipse" /> ?
>
> The names you specify as you have discovered will appear as fields from your
> code-behind.
>
> However, that should still work. I'm guessing you are missing a namespace
> import.
>
> Do you have "using System.Windows.Shapes;" ?
>
> - Doug
>
> > Compiler says that Ellipse is a 'field' but is used like a 'type'? I
> > do have an Ellipse in my xaml so I suppose it thinks I'm trying to
> > instantiate an instance rather than a class definition? But isn't
> > Ellipse a WPF type? How do I refer to the type?
> >
> > Thanks!
> >
> > Peter
> >
> > "Drew Marsh" wrote:
> >
> >> peter wrote:
> >>
> >>> I would like to create WPF objects programmatically. Does anyone
> >>> know of a tutorial for this? For example, I would to do something
> >>> like...
> >>>
> >>> Ellipse myEllipse = new Ellipse();
> >>>
> >>> Unfortunately, I can't get this syntax to compile. Ideally I would
> >>> be able to create and define a particular kind of object in XAML/C#
> >>> and then instantiate it. It appears like I need to create a resource
> >>> defintion in XAML and associate it with my codebehind? I'm not
> >>> finding examples of this and haven't been able to hack something
> >>> together on my own.
> >>>
> >>> I tried the free cell sample, but this implementation seems rather
> >>> abstruse to me. Can anyone point me to a more basic example? Thanks.
> >>>
> >> You should definitely be able to do exactly what you're trying to do.
> >> It's a very straightforward programming model. What erros are you
> >> getting?
> >>
> >> -Drew
> >> ___________________________________
> >> Drew Marsh
> >> Chief Software Architect
> >> Mimeo.com, Inc. - http://www.mimeo.com
> >> Microsoft C# / WPF MVP
> >> Weblog - http://blog.hackedbrain.com/

>
>
>

My System SpecsSystem Spec
Closed Thread
Update your Vista Drivers Update Your Drivers Now!!

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
datalist -- Object reference not set to an instance of an object. Deere .NET General 0 08-31-2008 09:09 AM
Compare-Object and Get the name of object/File? akcorr PowerShell 5 06-19-2008 05:15 AM
Testing object arrays using Compare-Object and -contains Alex K. Angelopoulos [MVP] PowerShell 2 08-31-2006 05:57 PM
Adding canonical aliases for Compare-Object, Measure-Object, New-Object Alex K. Angelopoulos [MVP] PowerShell 2 05-26-2006 07:58 AM



Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008