Attached Behavior

4 posts, 0 answers
  1. Jan
    Jan avatar
    5 posts
    Member since:
    Aug 2012

    Posted 11 Aug 2012 Link to this post

    Hi,

    I started evaluation JustCode and thus far I am very happy with it.

    One "Warning" that is reported erroneously IMHO is a XAML warning for "XAML: Unknown property 'X' of type 'Foo'"

    This happens if I define a standard Attached Behavior as follows:

    public class Foo
    {
        public static readonly DependencyProperty XProperty =
            DependencyPropery.RegisterAttached
                 ("X", typeof(object), typeof(Foo), new UIPropertyMetaData(null));
     
        public static void SetX(DependencyObject target, object value)
        {
             target.SetValue(XProperty, value);
        }
     
        public static void GetX(DependencyObject target)
        {
            return target.GetValue(XProperty);
        }
    }


    I hope there are no nasty typos in the above example. We now have a basic attached property wrapped in a class. This behavior obviously does nothing, but we don't need that for the example.

    JustCode will now generate the warning cited above (namespacing etc. omitted for brevity)
    XAML: Unknown property 'X' of type 'Foo'

    The issue is obviously that JustCode is looking for the CLR properties to back the X property - but those are not present in the case of attached properties / attached behaviors.
    See also http://msdn.microsoft.com/en-us/library/ms749011.aspx 

    Or am I missing something really basic here.

    Cheers,
    Jan
  2. Ivan
    Admin
    Ivan avatar
    106 posts

    Posted 14 Aug 2012 Link to this post

    Hi,

    Thank you for your feedback. I tried to reproduce your issue, but I was not able to.
    Could you try refreshing the code analysis from JustCode->Analysis->Refresh Code Analysis. If the warning is still shown after the refresh, could you send me a sample solution that reproduces this issue?
    Thank you for your help!

    All the best,
    Ivan
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. DevCraft banner
  4. Jan
    Jan avatar
    5 posts
    Member since:
    Aug 2012

    Posted 14 Aug 2012 Link to this post

    Upon further investigation, the warning occurs with write-only Dependency properties.

    I sat down and created an example for you to see -- attached.

    Rename the attached file to zip -- it contains the example solution that ehibits the problem boiled down very narrowly.

    Cheers,
    Jan

    PS: doesn't let me attach zip files or a renamed zip file;
    so here's the code:

    Maindow.xaml
    <Window x:Class="WpfApplication1.MainWindow"
            xmlns:ab="clr-namespace:AttachedBehaviors"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <TextBox x:Name="m_tb1"/>
            <TextBox ab:SomeBehavior.X="{Binding m_tb1}"/>
        </Grid>
    </Window>

    SomeBehavior.cs
    namespace AttachedBehaviors
    {
        using System;
        using System.Linq;
        using System.Windows;
     
        public class SomeBehavior
        {
            public static readonly DependencyProperty XProperty =
                DependencyProperty.RegisterAttached("X", typeof(UIElement), typeof(SomeBehavior), new UIPropertyMetadata(XChanged));
     
            public static void SetX(DependencyObject target, UIElement value)
            {
                target.SetValue(XProperty, value);
            }
     
            private static void XChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
            {
                // do things :)
            }
        }
    }

    All other code was unchanged from the WPF app skeleton.

    Cheers,
    Jan
  5. Svetlozar
    Admin
    Svetlozar avatar
    271 posts

    Posted 17 Aug 2012 Link to this post

    Hello,

    Thank you for the example provided! I logged it in our system for further investigation.

    All the best,
    Svetlozar
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Back to Top