User avatar
ARPP3
Topic Author
Posts: 26
Joined: 20 Jun 2023, 21:22

DependencyProperty Setup Issues

14 Jul 2023, 12:50

Good afternoon,

I am still quite new to Noesis and XAML in general, and I have not been able to set up a custom property for use in my element successfully. I am wondering what I've done wrong or missed. I've searched the Inventory and SolarSystem samples to try and learn, I feel as though I am quite close since this works in the local designer.

Here's the code-behind for TestListElement.xaml
#if UNITY_5_3_OR_NEWER
#define NOESIS
using Inventory;
using Noesis;
#else
using System;
using System.Windows;
using System.Windows.Controls;
#endif

namespace Test
{
    public partial class TestListElement : UserControl
    {
        public string Time
        {
            get { return (string)GetValue(TimeProperty); }
            set { SetValue(TimeProperty, value); }
        }

        public static readonly DependencyProperty TimeProperty = DependencyProperty.Register(
            "Time", typeof(string), typeof(TestListElement),
            new PropertyMetadata((string)"0:00.000"));

        public TestListElement()
        {
            InitializeComponent();
        }

#if NOESIS
        private void InitializeComponent()
        {
            NoesisUnity.LoadComponent(this);
        }
#endif
    }
}
And here's a simplified minimal repro of the xaml itself
<UserControl x:Class="Testing.TestListElement" x:Name="Root"
             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" d:DesignHeight="70" d:DesignWidth="1500"
             xmlns:local="clr-namespace:Testing"
             d:DataContext="{d:DesignInstance {x:Type local:TestListElement}, IsDesignTimeCreatable=True}">
    <Grid>

        <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center"
                   FontFamily="{StaticResource Font.Raleway}" FontSize="30" FontWeight="Black"
                   Text="{Binding Time}"/>
    </Grid>
</UserControl>
In Blend for VS I can see the default value of 0:00.000 in the designer for TestListElement.xaml. This works exactly as expected 🙂

When I use this element in another xaml file, I cannot see the default value, and also I cannot set the value. Everything else will draw as expected (I culled my code as a minimal repro but much more is going on which works fine), except for the contents of this text block. I have no compiler errors, or runtime errors in Unity. Have I missed some critical dependency step, or missed the naming convention when referencing this binding?

For added context, here's a demo of my use-case in another xaml file
<UserControl x:Class="Testing.TestMenu"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:noesis="clr-namespace:NoesisGUIExtensions;assembly=Noesis.GUI.Extensions"
             xmlns:local="clr-namespace:Testing"
             x:Name="Root">
    
    <Grid Background="#FF1C1D20">
        <StackPanel Grid.Column="0" Grid.Row="1" Margin="-20,230,470,0">
            <local:TestListElement/> <!-- Should be 0:00.000? As the default value -->
            <local:TestListElement Time="1:27.000"/> <!-- Also not working -->
        </StackPanel>
    </Grid>
</UserControl>
Sorry if the code doesn't make much sense, I deleted a bunch to keep the example as minimal as possible. Why is my text box not receiving the values I set, or even default values, outside of the local designer?

I feel as though once I get this concept I will never forget it, but something just hasn't clicked yet,
Thank you,
 
User avatar
ARPP3
Topic Author
Posts: 26
Joined: 20 Jun 2023, 21:22

Re: DependencyProperty Setup Issues

14 Jul 2023, 12:53

Oops! Please ignore the mismatch of the namespace in the code behind, it should be
namespace Testing
I did my best to cull and refactor my code to keep it relevant and minimal, I just messed that part up. Thanks!
 
User avatar
sfernandez
Site Admin
Posts: 3363
Joined: 22 Dec 2011, 19:20

Re: DependencyProperty Setup Issues

14 Jul 2023, 13:03

Hi,

If your intention is to show the value of the Time DP then you need to reference the control in the binding:
<UserControl x:Class="Testing.TestListElement" x:Name="TestListElementRoot"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" ...>
    <Grid>
        <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center"
                   FontFamily="{StaticResource Font.Raleway}" FontSize="30" FontWeight="Black"
                   Text="{Binding Time, ElementName=TestListElementRoot}"/>
    </Grid>
</UserControl>
If you only specify the name of the property, then the binding will be resolved against the current DataContext. This is why in the Blend designer was working, because you were setting an instance of the UserControl as design-time DataContext:
d:DataContext="{d:DesignInstance {x:Type local:TestListElement}, IsDesignTimeCreatable=True}"
Hope this helps.
 
User avatar
ARPP3
Topic Author
Posts: 26
Joined: 20 Jun 2023, 21:22

Re: DependencyProperty Setup Issues

14 Jul 2023, 13:29

This makes so much sense! Thank you a ton :)

Who is online

Users browsing this forum: No registered users and 5 guests