WPF non visual dcependancy property inheritance for non-visual objects

G

germ

Sorry if this is the wrong place to post ths question - feel free to point
me to the proper group if it isn't.

I am trying to implement WPF dependancy property inheritance in a non-visual
object hierarchy.
Below is the complete code for a simple console app test - it just requires
references to PresentationCore, PresentationFramework & WindowsBase to
compile.
I am stuck on how to implement the object relations.
How does WPF programatically implement containership to enable property
inheritance ?
It would be a shame if this is only possible with visual elements.

Gerry



using System;
using System.Windows;
namespace CTest
{

class Program
{

static Selectable a = new Selectable();
static Selectable aa = new Selectable();
static Selectable aaa = new Selectable();
static Selectable aab = new Selectable();
static Selectable ab = new Selectable();
static Selectable aba = new Selectable();
static Selectable abb = new Selectable();

static void Main( string[] args )
{
/*
*
* can we mimic xaml containment structure
* to enable dependancy property value inheritance
*
* a.AddChild(aa); ???????
* aa.AddChild(aaa);
* aa.AddChild(aab);
* a.AddChild(ab);
* ab.AddChild(aba);
* ab.AddChild(abb);
*
*/
dump( null , "All false");
dump( a , "All true ");
dump( aa , "aa branch true" );
dump( aaa , "aaa true" );
Console.Write( "Any key to continue ...." );
Console.ReadKey( true );
}

static void dump( Selectable s ,string msg)
{
if ( s != null )
s.Selected = true;
Console.WriteLine( "--- {0} ---" , msg );
Console.WriteLine( "a = {0}" , a.Selected );
Console.WriteLine( " aa = {0}" , aa.Selected );
Console.WriteLine( " aaa = {0}" , aaa.Selected );
Console.WriteLine( " aab = {0}" , aab.Selected );
Console.WriteLine( " ab = {0}" , ab.Selected );
Console.WriteLine( " aba = {0}" , aba.Selected );
Console.WriteLine( " abb = {0}" , abb.Selected );
Console.WriteLine();
if ( s != null )
s.ClearValue( Selectable.SelectedProperty );
}

public class Selectable: DependencyObject
{
public static readonly DependencyProperty SelectedProperty;
public bool Selected
{
get { return (bool) GetValue( SelectedProperty ); }
set { SetValue( SelectedProperty , value ); }
}
static Selectable()
{
SelectedProperty = DependencyProperty.Register(
"Selected" ,
typeof( bool ) ,
typeof( Selectable ) ,
new FrameworkPropertyMetadata(
false ,
FrameworkPropertyMetadataOptions.Inherits
)
);
}

}
}
 
G

germ

if anyone is interested here is the solution :


germ said:
Sorry if this is the wrong place to post ths question - feel free to point
me to the proper group if it isn't.

I am trying to implement WPF dependancy property inheritance in a
non-visual object hierarchy.
Below is the complete code for a simple console app test - it just
requires references to PresentationCore, PresentationFramework &
WindowsBase to compile.
I am stuck on how to implement the object relations.
How does WPF programatically implement containership to enable property
inheritance ?
It would be a shame if this is only possible with visual elements.

Gerry


using System;
using System.Windows;
namespace CTest
{

class Program
{

static Selectable a = new Selectable();
static Selectable aa = new Selectable();
static Selectable aaa = new Selectable();
static Selectable aab = new Selectable();
static Selectable ab = new Selectable();
static Selectable aba = new Selectable();
static Selectable abb = new Selectable();

static void Main( string[] args )
{
a.AddChild(aa);
aa.AddChild(aaa);
aa.AddChild(aab);
a.AddChild(ab);
ab.AddChild(aba);
ab.AddChild(abb);

dump( null , "All false");
dump( a , "All true ");
dump( aa , "aa branch true" );
dump( aaa , "aaa true" );
Console.Write( "Any key to continue ...." );
Console.ReadKey( true );
}

static void dump( Selectable s ,string msg)
{
if ( s != null )
s.Selected = true;
Console.WriteLine( "--- {0} ---" , msg );
Console.WriteLine( "a = {0}" , a.Selected );
Console.WriteLine( " aa = {0}" , aa.Selected );
Console.WriteLine( " aaa = {0}" , aaa.Selected );
Console.WriteLine( " aab = {0}" , aab.Selected );
Console.WriteLine( " ab = {0}" , ab.Selected );
Console.WriteLine( " aba = {0}" , aba.Selected );
Console.WriteLine( " abb = {0}" , abb.Selected );
Console.WriteLine();
if ( s != null )
s.ClearValue( Selectable.SelectedProperty );
}

public class Selectable: DependencyObject
{
public static readonly DependencyProperty SelectedProperty;
public bool Selected
{
get { return (bool) GetValue( SelectedProperty ); }
set { SetValue( SelectedProperty , value ); }
}
static Selectable()
{
SelectedProperty = DependencyProperty.Register(
"Selected" ,
typeof( bool ) ,
typeof( Selectable ) ,
new FrameworkPropertyMetadata(
false ,
FrameworkPropertyMetadataOptions.Inherits
)
);
}

public void Add( Selectable child )
{
Binding binding = new Binding("Selected");
binding.Source = this;
binding.Mode = BindingMode.OneWay;
binding.Converter = null;
BindingOperations.SetBinding( child , Selectable.SelectedProperty ,
binding );
}

}
}
 

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