Re: WPF non visual dcependancy property inheritance for non-visual objects
if anyone is interested here is the solution :
"germ" <germ2@newsgroup.nospam> wrote in message
news:eFZF4NCdKHA.2160@TK2MSFTNGP02.phx.gbl...
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 );
}
}
}