Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
DependencyProperty ClearValue
Message
De
18/02/2009 14:33:04
 
 
À
18/02/2009 14:16:20
Information générale
Forum:
ASP.NET
Catégorie:
Windows Presentation Foundation (WPF)
Versions des environnements
Environment:
C# 3.0
Divers
Thread ID:
01381612
Message ID:
01382731
Vues:
25
>>>I think I'm breaking your code. Don't have more time to play with it right now, but here's what I have:
>>>
>>>XAML:
>>><Window x:Class="WpfPropertyInheritance.Window1"
>>>    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
>>>    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>>>    xmlns:loc="clr-namespace:WpfPropertyInheritance"
>>>    Title="Window1" Height="300" Width="300">
>>>    <StackPanel>
>>>    <Button Height="23" Name="button1" Width="75" Click="button1_Click">Button</Button>
>>>    <Button Height="23" Name="button2" Width="75" Click="button2_Click">Button</Button>
>>>    <loc:ShapeCanvas3 x:Name="scTest" Height="172">
>>>      <StackPanel Canvas.Left="46" Canvas.Top="38" Height="100" Name="spnlTest" Width="200" >
>>>        <Rectangle Height="100" Name="rectangle1" Stroke="Black" Width="200" Fill="Azure" />
>>>      </StackPanel>
>>>    </loc:ShapeCanvas3>
>>>  </StackPanel>
>>></Window>
>>>
>>>
>>>C#:
using System.Windows;
>>>using System.Windows.Controls;
>>>using System.Diagnostics;
>>>
>>>namespace WpfPropertyInheritance
>>>  {
>>>  /// <summary>
>>>  /// Interaction logic for Window1.xaml
>>>  /// </summary>
>>>  public partial class Window1 : Window
>>>    {
>>>    public Window1()
>>>      {
>>>      InitializeComponent();
>>>      }
>>>
>>>    private void button1_Click(object sender, RoutedEventArgs e)
>>>      {
>>>      Debug.WriteLine("A1 Starting Value: " + scTest.ContentIsLocked.ToString());
>>>      scTest.ContentIsLocked = true;
>>>      Debug.WriteLine("A2 Set Canvas True: " + scTest.ContentIsLocked.ToString());
>>>      scTest.ContentIsLocked = true;
>>>      Debug.WriteLine("A3 Set Canvas True Again: " + scTest.ContentIsLocked.ToString());
>>>      scTest.ContentIsLocked = false;
>>>      Debug.WriteLine("A4 Set Canvas False: " + scTest.ContentIsLocked.ToString());
>>>      }
>>>
>>>    private void button2_Click(object sender, RoutedEventArgs e)
>>>      {
>>>      Debug.WriteLine("B2 Starting Values: " + scTest.ContentIsLocked.ToString()+ ":" + rectangle1.GetValue(ShapeCanvas3.ContentIsLockedProperty).ToString());
>>>      scTest.ContentIsLocked = true;
>>>      Debug.WriteLine("B2 Set Canvas True: " + scTest.ContentIsLocked.ToString() + ":" + rectangle1.GetValue(ShapeCanvas3.ContentIsLockedProperty).ToString());
>>>      rectangle1.SetValue(ShapeCanvas3.ContentIsLockedProperty, false);
>>>      Debug.WriteLine("B2 Set Rect False: " + scTest.ContentIsLocked.ToString() + ":" + rectangle1.GetValue(ShapeCanvas3.ContentIsLockedProperty).ToString());
>>>      }
>>>    }
>>>
>>>  public class ShapeCanvas3 : Canvas
>>>    {
>>>    public static DependencyProperty ContentIsLockedProperty;
>>>
>>>    static ShapeCanvas3()
>>>      {
>>>      FrameworkPropertyMetadata p = new FrameworkPropertyMetadata(
>>>        new PropertyChangedCallback(OnContentIsLockedChanged), 
>>>        new CoerceValueCallback(CoerceIsLocked)
>>>        );
>>>      p.Inherits = true;
>>>      ContentIsLockedProperty = DependencyProperty.RegisterAttached("ContentIsLocked", typeof(bool), typeof(ShapeCanvas3), p);
>>>      }
>>>
>>>    public bool ContentIsLocked
>>>      {
>>>      set {
>>>          Debug.WriteLine("SetVal: " + value.ToString());
>>>          if (value)
>>>            SetValue(ContentIsLockedProperty, value);
>>>          else
>>>            this.ClearValue(ContentIsLockedProperty);
>>>          }
>>>      get { return (bool)GetValue(ContentIsLockedProperty); }
>>>      }
>>>
>>>    private static void OnContentIsLockedChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
>>>      {
>>>      }
>>>
>>>    private static object CoerceIsLocked(DependencyObject d, object value)
>>>      {
>>>      if (d.ReadLocalValue(ContentIsLockedProperty) == DependencyProperty.UnsetValue)
>>>        {
>>>        Debug.WriteLine("Coerce 1: "+d.ToString()+":" + value.ToString());
>>>        return value;
>>>        }
>>>      bool b = (bool)value;
>>>      if (b)
>>>        {
>>>        Debug.WriteLine("Coerce 2: " + b.ToString());
>>>        return b;
>>>        }
>>>      else
>>>        {
>>>        Debug.WriteLine("Coerce 3: UnsetValue");
>>>        return DependencyProperty.UnsetValue;
>>>        }
>>>      }
>>>    
>>>    }  
>>>  }
>>>
>>>Here is the debug output on the second button:
>>>
>>>
>>>B2 Starting Values: False:False
>>>SetVal: True  
>>>Coerce 1: WpfPropertyInheritance.ShapeCanvas3:True
>>>Coerce 1: System.Windows.Controls.StackPanel:True
>>>Coerce 1: System.Windows.Shapes.Rectangle:True
>>>B2 Set Canvas True: True:True
>>>Coerce 1: System.Windows.Shapes.Rectangle:False
>>>B2 Set Rect False: True:False
>>>
>>>
>>>B2 Set Rect False: True:False = Canvas is locked, but child was able to be unlocked.
>>>
>>>Am I missing something?
>>
>>Hi,
>>I'm dragged back into VFP/ASP.NET for a couple of days so I can't try your version at the mo.....
>>I'll get back to you ASAP,
>>Viv

>I may have a solution to all of this, but I need to understand your usage a bit better. So let's see if I have this right.

Hi,
Just quick answers I'm afraid:

>ShapeCanvas3 is the root object.
Not neccessarily, but for the sake of this exercise - yes.

>If it is locked, all children are locked and no child can be unlocked.
Yes

>That's easy I have that coded.
>Now here's where the questions come in. Some options will be harder to code then others.
>Will ShapeCanvas3's children also have children that also need to be locked and unlocked?
Yes. With, from a logic POV, potentially unlimited nesting.

>And if ShapeCanvas3 becomes unlocked what should the behavior be:
>A. All children become unlocked.
>B. All children revert to their previously requested lock/unlock state.
>C. All children stay locked until they are explicitly unlocked.
If a child is explicitly locked it stays locked. If not it inherits from it's parent element so I think that means B.
(There should never be an explictly unlocked status)

>A I have coded, B I know how to code, C is going to be tricky, I think we will have to walk the tree in order to clear any previously unlocked DP off the children.

But if all children stayed locked unless explicitly unlocked then I wouldn't need property inheritance in the first place :-}

>I will probably have more questions based on your answer.
Fire away (g) - but, AFAICS, my current solution is working (well, I haven't been able to break it yet - i.e. end up with a element where the property is explicitly false).

Oh - and note that when using an attached property the same logic applies to nested elements other than ShapeCanvas3.
Best,
Viv
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform