Designerbehavior of custom control with DependencyProperty












0















I find a strange behavior of xaml designer with a DataGrid-CustomControl. There I have a DependencyProperty:



public static readonly DependencyProperty CustomizableColumnsProperty =
DependencyProperty.Register(
"CustomizableColumns",
typeof(ObservableCollection<DataGridColumn>),
typeof(DataGridCustomizable),
new PropertyMetadata(new ObservableCollection<DataGridColumn>()));


In the XAML-Designer I have the following code:



<ctrl:DataGridCustomizable
<ctrl:DataGridCustomizable.CustomizableColumns>
... the columns


In a overridden method



protected override void OnInitialized(EventArgs e)


I put the CustomizableColumns to the DataGrid Columns (only in DesignMode)



Now here is my notice. The first opening of the XAML designer, after a new build, perceives nothing from the CustomizableColumns. So in OnInitialized method, no any columns are added!



Then I close and reopen the XAML designer, and only just now the CustomizableColumns are known, and the OnInitialized method puts the CustomizableColumns to the DataGrid Columns.



Do you know the reason for it? Thanks for your input!










share|improve this question


















  • 1





    As a note, you must not set a default value of a mutable reference type dependency property by property metadata. All instances of your control would use the same ObservableCollection<DataGridColumn> object unless you explictly assign a property value. Besides that, do you really need the collection to be observable? Are collection members ever added or removed at runtime?

    – Clemens
    Nov 25 '18 at 17:09













  • Yes! You are absolutely right! I still have to try it, but that's probably the problem. Thank you for your comment. If I confirm it, could you write it again as an answer so that I can mark it as "correct" for posterity?

    – peter70
    Nov 26 '18 at 14:51
















0















I find a strange behavior of xaml designer with a DataGrid-CustomControl. There I have a DependencyProperty:



public static readonly DependencyProperty CustomizableColumnsProperty =
DependencyProperty.Register(
"CustomizableColumns",
typeof(ObservableCollection<DataGridColumn>),
typeof(DataGridCustomizable),
new PropertyMetadata(new ObservableCollection<DataGridColumn>()));


In the XAML-Designer I have the following code:



<ctrl:DataGridCustomizable
<ctrl:DataGridCustomizable.CustomizableColumns>
... the columns


In a overridden method



protected override void OnInitialized(EventArgs e)


I put the CustomizableColumns to the DataGrid Columns (only in DesignMode)



Now here is my notice. The first opening of the XAML designer, after a new build, perceives nothing from the CustomizableColumns. So in OnInitialized method, no any columns are added!



Then I close and reopen the XAML designer, and only just now the CustomizableColumns are known, and the OnInitialized method puts the CustomizableColumns to the DataGrid Columns.



Do you know the reason for it? Thanks for your input!










share|improve this question


















  • 1





    As a note, you must not set a default value of a mutable reference type dependency property by property metadata. All instances of your control would use the same ObservableCollection<DataGridColumn> object unless you explictly assign a property value. Besides that, do you really need the collection to be observable? Are collection members ever added or removed at runtime?

    – Clemens
    Nov 25 '18 at 17:09













  • Yes! You are absolutely right! I still have to try it, but that's probably the problem. Thank you for your comment. If I confirm it, could you write it again as an answer so that I can mark it as "correct" for posterity?

    – peter70
    Nov 26 '18 at 14:51














0












0








0








I find a strange behavior of xaml designer with a DataGrid-CustomControl. There I have a DependencyProperty:



public static readonly DependencyProperty CustomizableColumnsProperty =
DependencyProperty.Register(
"CustomizableColumns",
typeof(ObservableCollection<DataGridColumn>),
typeof(DataGridCustomizable),
new PropertyMetadata(new ObservableCollection<DataGridColumn>()));


In the XAML-Designer I have the following code:



<ctrl:DataGridCustomizable
<ctrl:DataGridCustomizable.CustomizableColumns>
... the columns


In a overridden method



protected override void OnInitialized(EventArgs e)


I put the CustomizableColumns to the DataGrid Columns (only in DesignMode)



Now here is my notice. The first opening of the XAML designer, after a new build, perceives nothing from the CustomizableColumns. So in OnInitialized method, no any columns are added!



Then I close and reopen the XAML designer, and only just now the CustomizableColumns are known, and the OnInitialized method puts the CustomizableColumns to the DataGrid Columns.



Do you know the reason for it? Thanks for your input!










share|improve this question














I find a strange behavior of xaml designer with a DataGrid-CustomControl. There I have a DependencyProperty:



public static readonly DependencyProperty CustomizableColumnsProperty =
DependencyProperty.Register(
"CustomizableColumns",
typeof(ObservableCollection<DataGridColumn>),
typeof(DataGridCustomizable),
new PropertyMetadata(new ObservableCollection<DataGridColumn>()));


In the XAML-Designer I have the following code:



<ctrl:DataGridCustomizable
<ctrl:DataGridCustomizable.CustomizableColumns>
... the columns


In a overridden method



protected override void OnInitialized(EventArgs e)


I put the CustomizableColumns to the DataGrid Columns (only in DesignMode)



Now here is my notice. The first opening of the XAML designer, after a new build, perceives nothing from the CustomizableColumns. So in OnInitialized method, no any columns are added!



Then I close and reopen the XAML designer, and only just now the CustomizableColumns are known, and the OnInitialized method puts the CustomizableColumns to the DataGrid Columns.



Do you know the reason for it? Thanks for your input!







c# wpf xaml dependency-properties






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 24 '18 at 23:38









peter70peter70

408720




408720








  • 1





    As a note, you must not set a default value of a mutable reference type dependency property by property metadata. All instances of your control would use the same ObservableCollection<DataGridColumn> object unless you explictly assign a property value. Besides that, do you really need the collection to be observable? Are collection members ever added or removed at runtime?

    – Clemens
    Nov 25 '18 at 17:09













  • Yes! You are absolutely right! I still have to try it, but that's probably the problem. Thank you for your comment. If I confirm it, could you write it again as an answer so that I can mark it as "correct" for posterity?

    – peter70
    Nov 26 '18 at 14:51














  • 1





    As a note, you must not set a default value of a mutable reference type dependency property by property metadata. All instances of your control would use the same ObservableCollection<DataGridColumn> object unless you explictly assign a property value. Besides that, do you really need the collection to be observable? Are collection members ever added or removed at runtime?

    – Clemens
    Nov 25 '18 at 17:09













  • Yes! You are absolutely right! I still have to try it, but that's probably the problem. Thank you for your comment. If I confirm it, could you write it again as an answer so that I can mark it as "correct" for posterity?

    – peter70
    Nov 26 '18 at 14:51








1




1





As a note, you must not set a default value of a mutable reference type dependency property by property metadata. All instances of your control would use the same ObservableCollection<DataGridColumn> object unless you explictly assign a property value. Besides that, do you really need the collection to be observable? Are collection members ever added or removed at runtime?

– Clemens
Nov 25 '18 at 17:09







As a note, you must not set a default value of a mutable reference type dependency property by property metadata. All instances of your control would use the same ObservableCollection<DataGridColumn> object unless you explictly assign a property value. Besides that, do you really need the collection to be observable? Are collection members ever added or removed at runtime?

– Clemens
Nov 25 '18 at 17:09















Yes! You are absolutely right! I still have to try it, but that's probably the problem. Thank you for your comment. If I confirm it, could you write it again as an answer so that I can mark it as "correct" for posterity?

– peter70
Nov 26 '18 at 14:51





Yes! You are absolutely right! I still have to try it, but that's probably the problem. Thank you for your comment. If I confirm it, could you write it again as an answer so that I can mark it as "correct" for posterity?

– peter70
Nov 26 '18 at 14:51












1 Answer
1






active

oldest

votes


















1














You must not set a default value of a mutable reference type dependency property by property metadata. All instances of your control would use the same ObservableCollection<DataGridColumn> object unless you explictly assign a property value.



You should instead set the default value by a SetCurrentValue call in the control's constructor.



public static readonly DependencyProperty CustomizableColumnsProperty =
DependencyProperty.Register(
nameof(CustomizableColumns),
typeof(ObservableCollection<DataGridColumn>),
typeof(DataGridCustomizable));

...

public DataGridCustomizable()
{
SetCurrentValue(CustomizableColumnsProperty,
new ObservableCollection<DataGridColumn>());
}


Using SetCurrentValue instead of just SetValue ensures that any Binding, Style Setter or other dependency property value source still works correctly.






share|improve this answer
























  • Excellent! Thanks alot :-)

    – peter70
    Nov 27 '18 at 5:47











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53463362%2fdesignerbehavior-of-custom-control-with-dependencyproperty%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You must not set a default value of a mutable reference type dependency property by property metadata. All instances of your control would use the same ObservableCollection<DataGridColumn> object unless you explictly assign a property value.



You should instead set the default value by a SetCurrentValue call in the control's constructor.



public static readonly DependencyProperty CustomizableColumnsProperty =
DependencyProperty.Register(
nameof(CustomizableColumns),
typeof(ObservableCollection<DataGridColumn>),
typeof(DataGridCustomizable));

...

public DataGridCustomizable()
{
SetCurrentValue(CustomizableColumnsProperty,
new ObservableCollection<DataGridColumn>());
}


Using SetCurrentValue instead of just SetValue ensures that any Binding, Style Setter or other dependency property value source still works correctly.






share|improve this answer
























  • Excellent! Thanks alot :-)

    – peter70
    Nov 27 '18 at 5:47
















1














You must not set a default value of a mutable reference type dependency property by property metadata. All instances of your control would use the same ObservableCollection<DataGridColumn> object unless you explictly assign a property value.



You should instead set the default value by a SetCurrentValue call in the control's constructor.



public static readonly DependencyProperty CustomizableColumnsProperty =
DependencyProperty.Register(
nameof(CustomizableColumns),
typeof(ObservableCollection<DataGridColumn>),
typeof(DataGridCustomizable));

...

public DataGridCustomizable()
{
SetCurrentValue(CustomizableColumnsProperty,
new ObservableCollection<DataGridColumn>());
}


Using SetCurrentValue instead of just SetValue ensures that any Binding, Style Setter or other dependency property value source still works correctly.






share|improve this answer
























  • Excellent! Thanks alot :-)

    – peter70
    Nov 27 '18 at 5:47














1












1








1







You must not set a default value of a mutable reference type dependency property by property metadata. All instances of your control would use the same ObservableCollection<DataGridColumn> object unless you explictly assign a property value.



You should instead set the default value by a SetCurrentValue call in the control's constructor.



public static readonly DependencyProperty CustomizableColumnsProperty =
DependencyProperty.Register(
nameof(CustomizableColumns),
typeof(ObservableCollection<DataGridColumn>),
typeof(DataGridCustomizable));

...

public DataGridCustomizable()
{
SetCurrentValue(CustomizableColumnsProperty,
new ObservableCollection<DataGridColumn>());
}


Using SetCurrentValue instead of just SetValue ensures that any Binding, Style Setter or other dependency property value source still works correctly.






share|improve this answer













You must not set a default value of a mutable reference type dependency property by property metadata. All instances of your control would use the same ObservableCollection<DataGridColumn> object unless you explictly assign a property value.



You should instead set the default value by a SetCurrentValue call in the control's constructor.



public static readonly DependencyProperty CustomizableColumnsProperty =
DependencyProperty.Register(
nameof(CustomizableColumns),
typeof(ObservableCollection<DataGridColumn>),
typeof(DataGridCustomizable));

...

public DataGridCustomizable()
{
SetCurrentValue(CustomizableColumnsProperty,
new ObservableCollection<DataGridColumn>());
}


Using SetCurrentValue instead of just SetValue ensures that any Binding, Style Setter or other dependency property value source still works correctly.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 26 '18 at 15:01









ClemensClemens

88.9k889180




88.9k889180













  • Excellent! Thanks alot :-)

    – peter70
    Nov 27 '18 at 5:47



















  • Excellent! Thanks alot :-)

    – peter70
    Nov 27 '18 at 5:47

















Excellent! Thanks alot :-)

– peter70
Nov 27 '18 at 5:47





Excellent! Thanks alot :-)

– peter70
Nov 27 '18 at 5:47




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53463362%2fdesignerbehavior-of-custom-control-with-dependencyproperty%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'