Access sub-properties in <Setter/>

Hi,

Suppose I have:

public class CustomPanel : StackPanel
{
public Image Image
{
get { return _image };
}
}

And I have this style:

<Style TargetType="{x:Type CustomPanel}">
<Setter Property="Image.Width" Value="100"/>
</Style>

That seems to set the width of the panel rather than the width of the image. How can I specifically target the image width and set the panel width separately I tried this but it gives a compile error:

<Style TargetType="{x:Type CustomPanel}">
<Setter Property="(Image).Width" Value="100"/>
</Style>

Thanks,
Kent Boogaart



Answer this question

Access sub-properties in <Setter/>

  • dezrtluver

    We're looking at it, but no plans for this version of WPF.

    The workaround that frequently works is to use a Storyboard. It's a lot more to type, but often ends up accomplishing the same result. The idea is to have the animation start right away, take effect immediately (not over time), and keep running forever, or in this case as long as the style is applied. In the below example, you can put any object type you want inside the <DiscreteObjectKeyFrame.Value>:

    <Page
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Background="White">

    <Page.Resources>
    <
    Style TargetType="{x:Type Button}">
    <
    Style.Triggers>
    <
    EventTrigger RoutedEvent="Button.Loaded">
    <
    BeginStoryboard>
    <
    Storyboard TargetProperty="Background.Color">
    <
    ObjectAnimationUsingKeyFrames >
    <
    DiscreteObjectKeyFrame KeyTime="0:0:0" >
    <
    DiscreteObjectKeyFrame.Value>
    <
    Color>Blue</Color>
    </
    DiscreteObjectKeyFrame.Value>
    </
    DiscreteObjectKeyFrame>
    </
    ObjectAnimationUsingKeyFrames>
    </
    Storyboard>
    </
    BeginStoryboard>
    </
    EventTrigger>

    </Style.Triggers>
    </
    Style>
    </
    Page.Resources>

    <Button Background="Red">Click</Button>

    </Page>


  • mjt

    Right about that!

    While I did think I actually used this syntax in Setters... it was in fact in DataTrigger!!!

    Sorry about that mistake and I hope I did not caused you too much pain, fooling around with Setters.



  • riemerg

    Unfortunately you can't access sub-properties in a Setter.Property or Trigger.Property (you can in Bindings and Storyboards, though).

    In the setter syntax, if you have Property="Foo.Bar", it's interpreted as "Class.Property". So your Setter is really setting the Image class' Width property, which is synonomous with Panel.Width, since they both inherit it from FrameworkElement.


  • Tailor

    I've done this countless times (acces sub properties) and it should work properly (although I did not use the exact same case as you).

    Have you tried changing the "Stretch" property of the Image so it can resize accordingly

    <Style TargetType="{x:Type CustomPanel}">
    <Setter Property="Image.Width" Value="100"/>
    <Setter Property="Image.Stretch" Value="Fill" /> <!-- or "UniformFill" if you want to preserve aspect ratio -->
    </Style>




  • Jiajia

    No harm done - I was asleep during both those replies :)

    That's an interesting limitation - are there plans to allow the same ability to access subproperties in setters as in bindings and storyboards

    Thanks,
    Kent


  • Access sub-properties in <Setter/>