-1

I have defined a readonly dependency property of type ToolBar to my control.

How can I populate this ToolBar with Items in xaml code? The ToolBar is created in constructor of my control and is readonly.

I can write

<ToolBar>
    <MenuItem/>
    <Button/>
</ToolBar>

but when I write

<my:myControl.MyToolBar>
    <MenuItem/>
    <Button/>
</my:myControl.MyToolBar>

the error says 'Property MyToolBar does not support values of 'Button/MenuItem''

redstripes
  • 87
  • 1
  • 9

1 Answers1

0

If you are creating a custom ToolBar control it should inherit ToolBar or HeaderedItemsControl

public class MyToolBar : HeaderedItemsControl
{
}

And if MyToolBar is a property of a control, you should set it to a ToolBar value:

<my:myControl.MyToolBar>
    <ToolBar>
        <MenuItem/>
        <Button/>
    </ToolBar>
</my:myControl.MyToolBar>

Edit:

I have defined a readonly dependency property of type ToolBar to my control. How can I populate this ToolBar with Items in xaml code?

You can't. This is not supported in XAML:

WPF: Cannot set items of a read-only collection in XAML

Remember that XAML is only a markup language.

Community
  • 1
  • 1
mm8
  • 163,881
  • 10
  • 57
  • 88
  • I forgot to mention, that the ToolBar which is set to MyToolBar gets created in MyControl-Constructor and is readonly, so this is not possible – redstripes May 19 '17 at 14:17
  • Then you can't populate the collection in XAML. This is not supported. – mm8 May 19 '17 at 14:21