-2
   <Grid Width="100">     

        <Grid.Style>
            <Style>
                <Setter Property="Ellipse.Height" Value="300"/>
                <Setter Property="Ellipse.Fill" Value="Blue"/>
                <Setter Property="Grid.Background" Value="Green"/>
            </Style>                
        </Grid.Style>

        <Ellipse Name="Elp" Width="100" Height="100"></Ellipse>

    </Grid>

Above code works as expected except for Ellipse.Fill . My guess is it is not attached property where as Ellipse.Height is . So, how to check if a property is attachable or not ?

Above code doesn't produce any error what so ever.

AnjumSKhan
  • 9,647
  • 1
  • 26
  • 38
  • I think the problem is you are trying to set `Ellipse.Fill` on `Grid`. – Aron Oct 23 '14 at 13:33
  • You can apply attached properties from another classes just fine, that's what they're for! I think the question is how can you find out if a property is an attached property. – Andy Oct 23 '14 at 14:54

3 Answers3

4

Fill property is defined in Shape class and not Grid. Your code tries to apply Fill property in Grid which is not valid and thus doesn't work.

Also be aware that Ellipse.Height property doesn't belongs to Ellipse but FrameworkElement. Grid is a framework element and thus it gets applied.

If you think <Setter Property="Ellipse.Height" Value="300"/> sets ellipse height to 300, that's not what happening here. It sets Grid.Height to 300.

Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
  • You say Fill property doesn't belong to Grid, then we don't get an error ? The code runs just fine. – AnjumSKhan Oct 23 '14 at 15:01
  • @AnjumSKhan There is an error. You just don't know where to look. Please turn on WPF binding errors in `Tools` `Options` `Debugging` `Output Window`. `` is a runtime dynamic class, which has no compile time checking. WPF does not stop execution on binding errors. – Aron Oct 23 '14 at 15:15
4

You are correct that it's not an attached property and so can't be applied to Grid, you can tell if a property is attached in the designer using the tooltip from intellisense

enter image description here

or if you're trying to find this out from code behind you could do something like

   bool IsAttachedProperty(DependencyProperty dependencyProperty)
    {
        DependencyPropertyDescriptor dependencyPropertyDescriptor = DependencyPropertyDescriptor.FromProperty(dependencyProperty, dependencyProperty.OwnerType);
        if (dependencyPropertyDescriptor != null && dependencyPropertyDescriptor.IsAttached)
            return true;
        else
            return false;
    }

used with the line

bool canvasLeft = IsAttachedProperty(Canvas.LeftProperty);
Andy
  • 6,366
  • 1
  • 32
  • 37
0

The above result can be obtained using

    <Grid Width="100">

        <Grid.Resources>
            <Style x:Key="grdRes">
                <Setter Property="Ellipse.Height" Value="300"/>
                <Setter Property="Ellipse.Fill" Value="Blue"/>
                <Setter Property="Grid.Background" Value="Green"/>
            </Style>
        </Grid.Resources>

        <Ellipse Name="Elp" Width="100" Height="100">
            <Ellipse.Style>
                <Style BasedOn="{StaticResource grdRes}"></Style>
            </Ellipse.Style>
        </Ellipse>

    </Grid>

Also after pressing spacebar attached properties' class names are shown with {} like if you type

<Button followed by spacebar

it shows {}Grid note curly brace before Grid which shows Grid is exposing attached properties for Button.

And interestingly, number of attached properties changes too depending upon visual tree.

<TextBox Width="100" Height="50" FontSize="30" Grid.IsSharedSizeScope="True">

If TextBox is not present inside a Grid, then Grid.Row is not attached and not shown in intellisense, where as IsSharedSizeScope is shown.

AnjumSKhan
  • 9,647
  • 1
  • 26
  • 38