Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 297217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:37:10+00:00 2026-05-12T06:37:10+00:00

How do I bind an element of a derived <UserControl> to an element of

  • 0

How do I bind an element of a derived <UserControl> to an element of the base <UserControl>?

I have defined a UserControl called Tile as my base class. This would be an abstract base class, if XAML wouldn’t balk when I tried to instantiate an object derived from it …

Anyway, so far Tile only contains a single dependency property: OuterShape. It is a System.Windows.Shapes.Shape object (also abstract). Thus: all tiles will have some kind of visual shape associated with them, for rendering. But there are many different types of Tiles — some with System.Windows.Shapes.Paths, some with System.Windows.Shapes.Polygons, etc.

So now I am working on the first derived UserControl: PolyTile. As the name implies, this is a Tile that uses a System.Windows.Shapes.Polygon as its “OuterShape” property.

The problem I am running into is that I can’t figure out how to successfully bind the XAML <Polygon> element to the Shape property in the base class.

/************
** Tile.cs **
************/
// This class contains a generic shape, and that's all.
// This class does NOT have a XAML file to go with it. It is purely code implemented.
public class Tile : System.Windows.Controls.UserControl
{
    public static readonly System.Windows.DependencyProperty OuterShapeProperty
        = System.Windows.DependencyProperty.Register(   "OuterShape",
                                                        typeof(System.Windows.Shapes.Shape),
                                                        typeof(Tile));
    public System.Windows.Shapes.Shape OuterShape
    {   get { return (System.Windows.Shapes.Shape)GetValue(OuterShapeProperty); }
        set { SetValue(OuterShapeProperty, (System.Windows.Shapes.Shape)value); }
    }
}

……………………

<!--*****************
*** PolyTile.xaml ***
******************-->
<local:Tile x:Name="__PolyTile__" x:Class="WPFApplication1.PolyTile"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WPFApplication1">
    <Grid Name="grdMain">
        <Polygon Name="OuterPoly" />
            <!--OuterPoly needs to be data-bound (OneWay)
            to the OuterShape property of the Tile UserControl.
            That way, when my derived class sets OuterShape to a new <Polygon> element,
            OuterShape will show up on the screen as a <Polygon>-->
    </Grid>
</local:Tile>

……………………

/****************
** PolyTile.cs **
****************/
// This class encapsulates a tile that is in the shape of a specified <Polygon> element.
/// <summary>
/// Interaction logic for PolyTile.xaml
/// </summary>
public partial class PolyTile : Tile // Derived from Tile
{
    public PolyTile()
    {
        InitializeComponent();

        System.Windows.Data.BindingExpressionBase BindExp;
        PolyConverter polyConverter = new PolyConverter();

        System.Windows.Data.Binding PolyBinding = new System.Windows.Data.Binding("OuterPoly");
        PolyBinding.Source = __PolyTile__;
        PolyBinding.Mode = System.Windows.Data.BindingMode.OneWayToSource;
        PolyBinding.Converter = polyConverter;
        BindExp = __PolyTile__.SetBinding(Tile.OuterShapeProperty, PolyBinding);
    }



    // The framework won't convert a base object into a derived object without an explicit cast ...
    // ... in this case, that means I have to make a custom DataConverter.
    [System.Windows.Data.ValueConversion(typeof(System.Windows.Shapes.Polygon), typeof(System.Windows.Shapes.Shape))]
    protected class PolyConverter : System.Windows.Data.IValueConverter
    {
        public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {    return value;    }
        // OneWayToSource binding: Thus the Convert method is never used.
        // Rather, ConverBack is used to cast the base <Shape> into a derived <Polygon>

        public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value.GetType() == typeof(System.Windows.Shapes.Polygon))
                return value;
            else
                return System.Windows.DependencyProperty.UnsetValue;
        }
    }
}

I don’t see any possible way to set this binding in XAML. (Although I don’t understand DataContexts well enough to say that I’ve thought of everything …)

Therefore, I tried setting the binding manually in the back-code. I’ve tried many different ways of organizing the binding path and source and target elements. Every time I get a fatal exception or a PathError or an UpdateSourceError something similar.

Does anyone know what I am doing wrong?
Any advice is greatly appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T06:37:10+00:00Added an answer on May 12, 2026 at 6:37 am

    It can be done using XAML object-syntax.

    It is important to note that the default data context works perfectly, and problems will arise if you attempt to get very explicit. For example: The binding will fail if you add ElementName="__PolyTile__" to the binding.

    It also appears that the data-converter is not required in this solution. That makes the code much easier to follow, though perhaps at the cost of a little type-saftey.

    <!--*****************
    *** PolyTile.xaml ***
    ******************-->
    <local:Tile x:Name="__PolyTile__" x:Class="WPFApplication1.PolyTile"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WPFApplication1">
    
        <local:Tile.OuterShape>
            <Binding Path="OuterPoly" Mode="OneWayToSource" />
        </local:Tile.OuterShape>
    
        <Grid Name="grdMain">
            <Polygon Name="OuterPoly" />
        </Grid>
    
    </local:Tile>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 241k
  • Answers 242k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer by your explanation it looks like your referring different objects,… May 13, 2026 at 7:31 am
  • Editorial Team
    Editorial Team added an answer I researched this topic about six months ago. Here's what… May 13, 2026 at 7:31 am
  • Editorial Team
    Editorial Team added an answer I believe the List would continue to hold a reference… May 13, 2026 at 7:31 am

Related Questions

I looked over this web and google and the solutions didn't work for me.
I'm just beinning basic data driven ASP.NET webforms design. I have the first part
So I've been working on this all day and I can't figure out how
A table row is generated using an asp:Repeater: <asp:repeater ID=announcementsRepeater OnItemDataBound=announcementsRepeater_ItemDataBound runat=Server> <itemtemplate> <tr

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.