1

Hallo and thank you for your time.

I have a peculiar problem. I have created a usercontol, which has some custom dependency properties.

If i implement the usercontrol, and bind to a static text. Everything is working fine.

However, if i try to set it to the value of a selected objects properties. It does not work.

This is the error i am getting in the output window:

Error: BindingExpression path error: 'SelectedUseCase' property not found on 'Helper.UserControls.UseCasePropertyDisplay'. BindingExpression: Path='SelectedUseCase.Name' DataItem='Helper.UserControls.UseCasePropertyDisplay'; target element is 'Helper.UserControls.UseCasePropertyDisplay' (Name='null'); target property is 'Text' (type 'String')

UserControl:
https://github.com/Toudahl/SoftwareDesignHelper/blob/master/Helper/UserControls/DisplayAndEditControl.xaml

<UserControl
    x:Class="Helper.UserControls.UseCasePropertyDisplay"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Helper.UserControls"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300"
    d:DesignWidth="400"
    DataContext="{Binding RelativeSource={RelativeSource Self}}">

    <StackPanel Orientation="Horizontal"
                Margin="260,0,0,0">
        <TextBlock Text="{Binding Label}"
                   Style="{StaticResource UseCaseTextBlock}"
                   x:Name="textblock_label"/>
        <TextBlock x:Name="textblock_propertyContent"
                   Text="{Binding Text}"
                   Style="{StaticResource UseCaseFrameWorkTextElement}"
                   DoubleTapped="textblock_DoubleTapped" />
        <TextBox x:Name="textbox_propertyContent"
                 Text="{Binding Text}"
                 Visibility="Collapsed"
                 Style="{StaticResource UseCaseTextBox}"
                 LostFocus="textbox_LostFocus" />
    </StackPanel>
</UserControl>

Decleration of dependency properties in codebehind:
https://github.com/Toudahl/SoftwareDesignHelper/blob/master/Helper/UserControls/DisplayAndEditControl.xaml.cs

    public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
            "Text",
            typeof(string),
            typeof(UseCasePropertyDisplay),
            new PropertyMetadata(null));

    public static readonly  DependencyProperty LabelProperty = DependencyProperty.Register(
        "Label",
        typeof(string),
        typeof(UseCasePropertyDisplay),
        new PropertyMetadata(null));

    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set {SetValue(TextProperty, value);}
    }

    public string Label
    {
        get { return (string)GetValue(LabelProperty); }
        set { SetValue(LabelProperty, value);}
    }

This is how i implement it on in the view:
https://github.com/Toudahl/SoftwareDesignHelper/blob/master/Helper/ViewsAndViewModels/ViewUseCases.xaml

<uc:UseCasePropertyDisplay Label="Name" Text="{Binding SelectedUseCase.Name, Mode=TwoWay}" />

From reading very similar questions here, im guessing that it has something to do with the way i set the context. However, the solution that has been provided to people (setting the relative source to the ancestor), doesnt work for me. Since its not available on my platform. I am not really sure where to go from here, as this is the first time i try to use usercontrols, and the first time i use dependency properties. School doesnt start untill a few weeks, so i cant get a hold of my teacher for this.

Morten Toudahl
  • 452
  • 1
  • 4
  • 13

1 Answers1

0

Instead of setting the DataContext global, set the first ui element to the inherited DataContext: DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"

As you have written, you can not use FindAncestor so you can try using a name and reference to it:

<UserControl
    x:Class="Helper.UserControls.UseCasePropertyDisplay"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Helper.UserControls"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300"
    d:DesignWidth="400"
    x:Name="Root">

    <StackPanel DataContext="{Binding ElementName=Root}"                                
                Orientation="Horizontal"
                Margin="260,0,0,0">
        <TextBlock Text="{Binding Label}"
                   Style="{StaticResource UseCaseTextBlock}"
                   x:Name="textblock_label"/>
        <TextBlock x:Name="textblock_propertyContent"
                   Text="{Binding Text}"
                   Style="{StaticResource UseCaseFrameWorkTextElement}"
                   DoubleTapped="textblock_DoubleTapped" />
        <TextBox x:Name="textbox_propertyContent"
                 Text="{Binding Text}"
                 Visibility="Collapsed"
                 Style="{StaticResource UseCaseTextBox}"
                 LostFocus="textbox_LostFocus" />
    </StackPanel>
</UserControl>
Juergen
  • 151
  • 1
  • 5
  • Thank you. This worked :) Can you explain to me why? To me this seems quite similar to setting the relativesource to self. And if you have a link to documentation or msdn tutorial/howto, i would be gratefull. I have not been able to find anything other than documentation for the relativesource class. – Morten Toudahl Aug 17 '15 at 06:25
  • if you are setting the DataContext to self, a instance of your UserControl class will be the DataContext. You have no chance to address properties from the outer ViewModel you are using. Without setting the DataContext in the UserControl it will use the parent one which you want. See the answer from pdross [http://stackoverflow.com/questions/5077377/usercontrols-datacontext](http://stackoverflow.com/questions/5077377/usercontrols-datacontext) for further information. – Juergen Aug 17 '15 at 09:22