Idov
Idov

Reputation: 5124

Send parameters to control defined in XAML

Is it possible to send parameters to a control defined in a XAML?
For example if I have this XAML:

<UserControl x:Class="Controls.MyControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d">
    <UserControl.Resources>
        <ResourceDictionary Source="..\Colors.xaml" />
    </UserControl.Resources>
    <Grid>
        <Polygon x:Name="plgPoly" Points="0,0 100,0 100,100 0,0" />

    </Grid>
</UserControl>

But when I construct my "Polygon" I want to give it coordinates which depend on the control which contain it (like : this.Height, this.Width, etc...) or on other controls defined in the parent control.
Is it possible? how?

Upvotes: 0

Views: 150

Answers (2)

Lukasz M
Lukasz M

Reputation: 5723

If you want to use only a simple value or bind to a custom property of owner control, use dependency property mechanism as suggested by H.B.. It is, however, probable, that you would need to convert a value from another type to appropriate one (i.e. PointCollection). In this case, I would suggest using a binding and a value converter to do it. More on value converters can be found here: http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.aspx.

Upvotes: 2

brunnerh
brunnerh

Reputation: 185290

If the property that should reference another is a dependency property that can easily be done via a Binding (with RelativeSource), otherwise you may need to construct your own MarkupExtension that fetches the value once (rather than binding it).

Upvotes: 2

Related Questions