user1139216
user1139216

Reputation: 119

Binding error on WPF (biding to DataContext)

First of all let me say that I'm rather new to WPF, so excuse me for any silly mistakes, but I've been cracking my head at this for a while now.

I have a simple sollution with three classes:

public class MyCustomItem

public class MyCustomLayout : ThirdPartyLayout<MyCustomItem>

public class MyViewController : INotifyPropertyChanged

MyCustomItem is a simple class with some properties ("Name" being one of them). ThirdPartyLayoutTool is a generic component inside an SDK that inherits from System.Windows.Controls.Panel. And MyViewController is the view controller I'm using as a data content.

I then created this simple XAML as the projects main window:

<Window x:Class="DependencyViewer.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sdk="clr-namespace:Sdk;assembly=Sdk"
    xmlns:local="clr-namespace:MyNamespace"
    Title="MainWindow" Height="350" Width="525">


    <local:MyCustomLayout x:Name="myLayout"/>
</Window>

Everything displays accordingly.Now my objective is to enhance the display of one of the sub components that is displayed by the ThirdPartyLayout panel, called TargetControl. So I add the following code:

<Window.Resources>
    <Style TargetType="{x:Type sdk:TargetControl}">
        <Style.Resources>
            <ToolTip x:Key="ToolTipContent">
                <StackPanel>
                    <TextBlock FontWeight="Bold" Text="Testing 1 2 3"/>
                    <TextBlock Text="{Binding Name}"/>
                </StackPanel>
            </ToolTip>
        </Style.Resources>
        <Setter Property="ToolTip" Value="{StaticResource ToolTipContent}"/>
    </Style>
</Window.Resources>

When I run the code, the "Testing 1 2 3" message appears correctly, however, I don't see the Name property. On the output window, I get the following message:

BindingExpression path error: 'Name' property not found on 'object' ''MyViewController' (HashCode=31884011)'

What I don't get is why the binding is happening on the MyViewController class, instead of TargetControl class. Any ideas?

Best regards, Carlos Jourdan

EDIT:

After tinkering guide mainly by the recommendations given by newb, I eventually found out that the source of the error is in fact in the SDK. The current release is still faulty, but when compiling from source I get the expected behavior.

Thanks for the help.

Upvotes: 0

Views: 1223

Answers (2)

newb
newb

Reputation: 856

When you create a binding in XAML, you are, by default, binding to the current DataContext. In this isntance, it seems that MyViewController is the DataContext of the sdk:TargetControl. To bind to the Name property of the skd:TargetControl instead, try the following:

    <TextBlock Text="{Binding Name, RelativeSource={RelativeSource AncestorType={x:Type sdk:TargetControl}}}"/>

Upvotes: 1

swcraft
swcraft

Reputation: 2112

Seems like DataContext of xaml.cs of MyViewController has the reference of MyCustomItem.

If you want you can set in xaml.cs, MyCustomLayout.ItemsSource = this.DataContext.

Or you can do MyCustomLayout.ItemsSource = specific property of MyCustomItem.

Upvotes: 1

Related Questions