Click here to Skip to main content
15,393,955 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello,

I have two partial classes dependent on a base class and am attempting to bind their windows to the base class via xaml. When I bind independent of the base class, the syntax used is relativesource self. How can I achieve this in the case of the base class.

Below is the syntax for binding independent of the base class which works fine.
DataContext="{Binding RelativeSource={RelativeSource Self}}"



Thanks
Posted
Comments
Mark Salsbery 27-May-11 13:52pm
   
An object of a class includes the members of its base class. Why then is the binding any different?
Member 7796817 27-May-11 14:22pm
   
I think with RelativeSource set to self may imply the properties specific to that window and not to the base class (correct me if I'm wrong). What is understood is that prior to the inclusion of the base class, the above mentioned syntax allowed the binding updates without issue.
Mark Salsbery 27-May-11 14:28pm
   
I may be missing something,but I had no idea what the base class was a base class of....the window class? Some other class? If it's some other class then provide a property in the window class that exposes an object of the other class type....then you can bind to that property and get at its members and base class members.
Member 7796817 27-May-11 14:36pm
   
Let me clarify. What I have is a base class that inherits from "Window" and holds the properties and methods of two windows which inherit their methods from the base class. The windows have both .xaml.cs and .xaml files and the base class is just .cs. Prior to including the base class as a parent structure, the windows had their properties in their respective classes and were bound to self as a result (relativesource self). However with the inheriting class, I seek to bind the windows to the parent class as "relativesource self" doesn't seem to perform as before.

Mark Salsbery 27-May-11 15:03pm
   
Thank you. I still don't see any difference. Most of the properties in the Window classs itself are actually in base classes like Control and UIElement. Your Window-derived "base class" should be no different. Your two classes derived from that class should be no different. I'm going to post a code sample in a solution below....see if this is the same as what you're doing...
Member 7796817 27-May-11 20:03pm
   
Excellent stuff Mark, actually as you mentioned the derived class inherits all including the binding. Thus the problem lay in a method that I didn't implement and this concerned the deserialization. Thanks for the help.

1 solution

Is this the same hierarchy you are using? This works fine. Note I'm using your DataContext binding to self, and accessing properties from both the base and the derived classes...

C#
namespace WPFTester
{
    public class BaseClass : Window
    {
        public string BaseClassString { get; set; }

        public BaseClass()
        {
            BaseClassString = "BaseClassString";
        }
    }

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : BaseClass
    {
        public string DerivedClassString { get; set; }

        public MainWindow()
        {
            DerivedClassString = "DerivedClassString";
            InitializeComponent();
        }
    }
}


<local:BaseClass x:Class="WPFTester.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WPFTester"
        Title="MainWindow" Height="350" Width="525"
        DataContext="{Binding RelativeSource={RelativeSource Self}}" >
    <Grid>
        <StackPanel >
            <TextBlock Text="{Binding Path=BaseClassString}" />
            <TextBlock Text="{Binding Path=DerivedClassString}" />
        </StackPanel>
    </Grid>
</local:BaseClass>
   
Comments
parmar_punit 28-May-11 0:19am
   
good answer my 5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900