Click here to Skip to main content
15,902,817 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I try set a guid or text (string) on my own control on xaml, but not work.

Code:
public class MyLabel : Label
    {

        [Category("Guid")]
        //[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public static DependencyProperty MyControlGuidProperty =
            DependencyProperty.Register("MyControlGuid", typeof(Guid), typeof(MyLabel));

        [Category("Guid")]        
        public Guid MyControlGuid
        {
            get
            {

                return (Guid)GetValue(MyControlGuidProperty);
            }
            set
            {
                SetValue(MyControlGuidProperty, value);
            }
        }

        public MyLabel()
            : base()
        {
            SetValue(MyControlGuidProperty, Guid.NewGuid());
        }

    }


But doesn't works. When i drag the new control form toolbox to MainWindows.xaml - generated the MyLabel but not MyControlGuid on Xaml. Under properties have the MyLabel a valied guid.

Only when i click at box at the end of the MyControlGuid - i can choose "convert to local value" and the guid was set it on xaml.

I get only
<local:MyLabel Content="MyLabel" HorizontalAlignment="Left" Margin="193,79,0,0" VerticalAlignment="Top"/>


but i want

<local:MyLabel Content="MyLabel" HorizontalAlignment="Left" Margin="193,79,0,0" VerticalAlignment="Top" MyControlGuid="6c090537-4a06-41d5-a0ea-6d3112df0f77"/>


this i get only over WPF Property Grid and click convert to local value.


What i must to do for that in code on my control (convert to local value)?

What I have tried:

I have try with the Attribute
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]

but not works.

I have try PropertyChangedCallback and CoerceValueCallback - not works too.
Posted
Updated 7-Jan-21 20:30pm
v2

1 solution

In the MyLabel constructor, it's simply:
MyControlGuid = Guid.NewGuid();
 
Share this answer
 
Comments
Member 13324229 8-Jan-21 2:52am    
Thank you very much for the answer. However, there is a misunderstanding here. The goal is when you drag the MyLabel from toolbox and drop on xaml UI, must be generated the MyControlGuid="6c090537-4a06-41d5-a0ea-6d3112df0f77" on xaml file. Under Property Grid for ui control contains already the right guid, but is not on xaml file. Only when you convert manually to local value over Visual Studio - fill the MycontrolGuid on xaml.

We can discuss which is better set Guid over clr Property or SetValue to Dependency Properties but boths ways make no difference for writing on xaml.

MyControlGuid should be behavior like Content or HorizontalAlignment of label and must be autogenerated on xaml by dropping on ui.

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