Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > Avalon

Vista - Programmatic object creation

 
 
Old 05-02-2006   #1 (permalink)
Peter


 
 

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


 
 

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


 
 

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


 
 

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


 
 

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
 

Thread Tools


Similar Threads
Thread Forum
Inherit from usercontrol - Object not set to instance of an object .NET General
Failing Object creation from vbs-script on Windows Server 2008 VB Script
datalist -- Object reference not set to an instance of an object. .NET General
Testing object arrays using Compare-Object and -contains PowerShell
Adding canonical aliases for Compare-Object, Measure-Object, New-Object PowerShell


Vista Forums 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 Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46