I have a WinForm application in which I am trying to replace my toolbar with a WPF toolbar. My solution now contains two projects:
- A WPF user control project that defines the WPF toolbar
- A Windows Form Application that hosts the WPF toolbar
Enabling / disabling buttons, adding items to combos, events handling of toolbar controls will have to be defined in the WinForm application, so in order to have access to them, I did the following:
- in the XAML I gave each control in the toolbar a name
- In the code I defined a public get property for each control
This works fine, but I was wondering if this is the right approach. Is there a better way to do what I want?
Can you post the XAML of your toolbar? – HighCore
I've made a small example of the XAML of my toolbar as you asked.
<ToolBarTray>
<ToolBar Band="1" BandIndex="1">
<Button Name="btnDoSomething1">
<Image Source="/WpfExampToolbarCtrl;component/Images/DoSomething1.png" />
</Button>
<Button Name="btnDoSomething2">
<Image Source="/WpfExampToolbarCtrl;component/Images/DoSomething2.png" />
</Button>
<Separator />
<Menu>
<MenuItem Header="Create Item">
<MenuItem Header="Item 1" />
<MenuItem Header="Item 2" />
</MenuItem>
</Menu>
<Separator />
<ComboBox Name="comboCategory">
<ComboBoxItem Content="Category 1" IsSelected="True" />
<ComboBoxItem Content="Category 2" />
</ComboBox>
</ToolBar>
</ToolBarTray>
I thought to give each control in the toolbar a name, and in the code behind define a get property for each one. This way I could access each of the toolbar's controls from my the main form of my WinForm application, and do what I want. (Add events, disable/enable controls at runtime, add items to combos at initialization or during runtime, …). But from what I understand from Kent Boogaart answer this is not the right approach.