C# and WPF

M

mick

Just started experimenting with WPF and was wondering if someone knows
how to reference properties from a WPF project in C#.

Description:

I have an oldish program I wrote in C#. Part of it is a list of buttons that
will launch other apps. Ive created a little animated information popout
in WPF that contains and image and a bit of text. Ive imported this project
into the solution that contains the C# project. The idea is when the
user mouses over one of the buttons then the popout ...erm... pops out.
Obviously which image and text that appears in the popout will depend
on which button is being moused over.
The WPF layout is -

Window
Border
StackPanel
Image
TextBlock

All works well except I dont know to reference the Image or TextBlock from
the
C# part to insert the appropriate image and text (Note: I'm not talking
about
the C# backing code here). When I new up an instance of the popout I can
access
all of the other properties; Position Colors etc but the things Ive added
dont
appear. This is probably a very basic question and probably more WPF than C#
but I thought I'd give it a go.

mick
 
J

James A. Fortune

Just started experimenting with WPF and was wondering if someone knows
how to reference properties from a WPF project in C#.

Description:

I have an oldish program I wrote in C#. Part of it is a list of buttons that
will launch other apps. Ive created a little animated information popout
in WPF that contains and image and a bit of text. Ive imported this project
into the solution that contains the C# project. The idea is when the
user mouses over one of the buttons then the popout ...erm... pops out.
Obviously which image and text that appears in the popout will depend
on which button is being moused over.
The WPF layout is -

Window
   Border
      StackPanel
          Image
          TextBlock

All works well except I dont know to reference the Image or TextBlock from
the
C# part to insert the appropriate image and text (Note: I'm not talking
about
the C# backing code here).  When I new up an instance of the popout I can
access
all of the other properties; Position Colors etc  but the things Ive added
dont
appear. This is probably a very basic question and probably more WPF thanC#
but I thought I'd give it a go.

mick

From:

WPF
Programmer's Reference
Windows Presentation Foundation With C# 2010 and .NET 4
ISBN 978-0-470-47722-9
by Rod Stephens
pp. 14-15

<Quote>

Property Binding to Provide Animation

In addition to using DirectX, another fundamental change with WPF is
the way it handles properties. WPF properties seem similar to to good
old Windows Forms properties at first, and, in many cases, you can
treat them similarly.

For example, to set a property in code, you simply create the value
that the property should have and assign it to the property. The
following C# code creates a LinearGradientBrush that shades from white
to green. It then sets the grdMain control's Background property to
the brush.

LinearGradientBrush bg =
new LinearGradientBrush(Colors.White, Colors.Green, 90);
grdMain.Background = bg;

Behind the scenes, however, WPF properties are very different from the
simple properties a program normally gives to the classes that it
creates. WPF uses 'Dependency Properties', a special kind of property
that is registered with the WPF property system. Dependency
properties support several features that normal properties do not,
including default values, inheritance (described further in the next
section), WPF-style data binding, and property change notification.

Change notification allows the WPF system to notice when a property
changes and take action if necessary. The WPF-style data binding
allows a property's value to be tied to some other value. Together
these two features allow a program to animate many of the properties
that define the appearance of its interface.

....

Property Inheritance

....

A different kind of inheritance

The idea of 'property inheritance' is different from the normal
concept of inheritance used in object-oriented programming (OOP). In
OOP, a derived class inherits the property, method, and event
definitions of the parent class but an object in the child class does
not inherit property values from the parent class. In WPF, a control
inherits some of the property values of the control that contains it.

....

The following list shows the precedence of the simplest places where a
property can get it value. The items at the top have the highest
precedence, so, for example, a Button's own properties (local values)
override inherited values, but an animation can override the local
values, at least while the animation is running.

1. Animated Values
2. Local value
3. Style
4. Inheritance
5. Default

For a more complete list of places where properties can get their
values, see

msdn.microsoft.com/ms743230.aspx

</Quote>

The author claims that all the source code for the book (in either VB
or C#) can be downloaded from:

www.vb-helper.com/wpf.htm

or by going to:

www.wrox.com

and finding the book by title or ISBN number and clicking on the
'Download Code' link.

James A. Fortune
(e-mail address removed)
 
M

mick

Peter Duniho said:
mick said:
[...]
All works well except I dont know to reference the Image or TextBlock
from the
C# part to insert the appropriate image and text (Note: I'm not talking
about
the C# backing code here). When I new up an instance of the popout I can
access
all of the other properties; Position Colors etc but the things Ive
added dont
appear. This is probably a very basic question and probably more WPF than
C#
but I thought I'd give it a go.

Hard to say without an actual concise-but-complete code example to work
with. But it sounds like you are confusing properties of an object with
child objects referenced by an object. The Image and TextBlock objects
are child objects of the StackPanel, not properties themselves.

I know. I couldnt even get a ref to the StackPanel to get to the Image and
TextBlock.
I solved it by creating public properties in the code behind and storing
references there.
They were then available to the main C# project.
Actually, you should be able to name them in the XAML (i.e. with the
"Name" attribute), so that you can access each directly from an
auto-generated field.

Yes I`d already given them an x:Name but that only made them visible to
the xaml projects codebehind.

But if you can't get that to work, you should be able to find the
references to the objects themselves in the StackPanel's Children object
(a UIElementCollection object).

Pete

Thanks for you reply,

mick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top