Re: Dynamically adding a new widget in C++ code, and then using FindName() to get it
I cannot overstate my appreciation for your on-going assistance. Thanks to your help maherne, I've managed to get the registration code to trigger. I also managed to place ElementRegistry in its own namespace (Ozmodiar) with the following change:
I've added the following cs dummy file:
While the Ozmodiar namespace seems to be accepted, I'm still getting the following errors preventing the xaml from being rendered in the blend designer:
I've attached the namespace to the canvas tag, and reference the dependency property as follows:
It seems I'm overlooking something, but I'm not clear as to what's missing...?
Gazoo
Code: Select all
NS_IMPLEMENT_REFLECTION( ElementRegistry, "Ozmodiar.ElementRegistry" )
Code: Select all
using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Markup;
using Microsoft.Xaml.Behaviors;
namespace Ozmodiar
{
class ElementRegistry : DependencyObject
{
#region IsRegistered property
public static readonly DependencyProperty IsRegisteredProperty = DependencyProperty.Register(
"IsRegistered", typeof(bool), typeof(ElementRegistry), new PropertyMetadata(false));
public bool IsRegistered
{
get { return (bool)GetValue(IsRegisteredProperty); }
set { SetValue(IsRegisteredProperty, value); }
}
#endregion
}
}
Code: Select all
Error XLS0415 The attachable property 'IsRegistered' was not found in type 'ElementRegistry'. noesis_sandbox_gui
Error XDG0044 The local property "IsRegistered" can only be applied to types that are derived from "ElementRegistry".
Code: Select all
...
xmlns:local2="clr-namespace:Ozmodiar"
...
<Button Height="45" Width="88" Canvas.Left="274" Canvas.Top="50" Content="Ajajaja" local2:ElementRegistry.IsRegistered="True" />
Gazoo
Re: Dynamically adding a new widget in C++ code, and then using FindName() to get it
I'm happy to help in any way I can, thanks!
In WPF/C# you need to use DependencyProperty.RegisterAttached to register attached properties, this should fix your Blend error. You can find documentation and examples here: https://learn.microsoft.com/en-us/dotne ... esktop-7.0. Note that these attached properties use static properties also.
In WPF/C# you need to use DependencyProperty.RegisterAttached to register attached properties, this should fix your Blend error. You can find documentation and examples here: https://learn.microsoft.com/en-us/dotne ... esktop-7.0. Note that these attached properties use static properties also.
Re: Dynamically adding a new widget in C++ code, and then using FindName() to get it
Thanks for the continued assistance! I managed to get the attached property properly recognized in Blend (C#). For others reading along, the example looks as follows:
One thing which I'm still wondering about, is namespace placement.
Noesis allows the ElementRegistry to be declared in a common or unique namespace via either of the following macro calls:
On the C# side however, the only way I've managed to place ElementRegistry in the common namespace, is as follows:
It is ultimately probably best to maintain custom elements in a new namespace, but I'm open to any suggestions to defining ElementRegistry in the common namespace. For example, is there a way to do it purely in the xaml file?
Cheers,
Gazoo
Code: Select all
namespace Ozmodiar
{
class ElementRegistry
{
// Register an attached dependency property with the specified
// property name, property type, owner type, and property metadata.
public static readonly DependencyProperty IsRegisteredProperty =
DependencyProperty.RegisterAttached(
"IsRegistered",
typeof(bool),
typeof(ElementRegistry)//,
//new FrameworkPropertyMetadata(defaultValue: false,
// flags: FrameworkPropertyMetadataOptions.AffectsRender)
);
// Declare a get accessor method.
public static bool GetIsRegistered(UIElement target) =>
(bool)target.GetValue(IsRegisteredProperty);
// Declare a set accessor method.
public static void SetIsRegistered(UIElement target, bool value) =>
target.SetValue(IsRegisteredProperty, value);
}
}
Noesis allows the ElementRegistry to be declared in a common or unique namespace via either of the following macro calls:
Code: Select all
NS_IMPLEMENT_REFLECTION( ElementRegistry, "Ozmodiar.ElementRegistry" )
NS_IMPLEMENT_REFLECTION( ElementRegistry )
Code: Select all
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "Ozmodiar")]
namespace Ozmodiar
{
...
Cheers,
Gazoo
Re: Dynamically adding a new widget in C++ code, and then using FindName() to get it
It is best to maintain custom elements in a new namespace as this is the expected behaviour for WPF/Blend. I do not believe there is a way to include types or namespaces in the default namespace using XAML.
Thanks for sharing your example!
Thanks for sharing your example!
Re: Dynamically adding a new widget in C++ code, and then using FindName() to get it
I try my best to 'pay it forward' when I get help. I.e. I may as well share the portions of work/code I manage to figure out so other readers can gain from it.
As for including something in the default namespace, I just wanted to emphasize that the approach I list above, which involves this line of code in the C# file:
does appear to accomplish this.
As you noted though, it's likely a wise move to always use a newly declared namespace as it's the only way to avoid accidentally conflicting with something already defined in the namespace.
Cheers,
Gazoo
As for including something in the default namespace, I just wanted to emphasize that the approach I list above, which involves this line of code in the C# file:
Code: Select all
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "Ozmodiar")]
As you noted though, it's likely a wise move to always use a newly declared namespace as it's the only way to avoid accidentally conflicting with something already defined in the namespace.
Cheers,
Gazoo
Who is online
Users browsing this forum: Ahrefs [Bot] and 3 guests