1
votes

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.

1
Can you post the XAML of your toolbar?Federico Berasategui

1 Answers

1
votes

I'd sooner have my WPF UI bound to a model. I would then access the model from the Winforms side and manipulate that.

To elaborate: WPF's binding infrastructure is very strong and unlike what you're used to in Winforms. WPF applications tend to follow the MVVM pattern, whereby the view's data context is a view model, and your controls in the view have bindings against properties in your view model.

Thus, what I'm suggesting is you first define a view model, then modify your WPF view to bind to properties on that view model, then access that view model from your Winforms code. Changing properties on that view model from your Winforms code will automatically update the WPF view.