WPF - Bind command declaratively to code behind when different from the datacontext - xaml

I'm not so great with WPF, so I am looking at a WPF control that someone else has created which has a datacontext set to another class, but also has tons of code in the code-behind on the actual control.
I need to create a context menu command that binds to a method inside of the code behind, not the actual datacontext of the control. I know how to create the context menu, just looking for how to bind to code behind when using another datacontext on the control.
Thanks!

Related

Code behind does not get called on dynamically created user controls

I am writing a WPF VMMV application.
- I am dynamically loading XAML into the window that contains user control.
- The user control has a textbox besides other simple controls
- The textbox has MouseDoubleClick and KeyDown events
Now, when I load a XAML which has my User Control, the control can be seen (as in the XAML designer), but the events won't work.
I have tried to make the user control with the MVVM pattern instead with code behind. That also does not work. Any experience with this or suggestions?

How Can I Pass An Event Handler Outside Of A Custom Class

I have created a custom Textbox using a new class of which inherits. To this custom Textbox I have added two buttons (embedded) inside the control which are both declared inside the class. One button is for Search and the other for Clear.
The purpose behind these buttons is to populate a Listview control when using Search and Clear the results when using clear...pretty straight forward.
However, my issue that I need help with is...
Because I have declared these buttons inside of my custom class, I cannot work out how to pass a 'Click' event outside of the class and back to the form. For example, if I type into the Textbox and hit the search button, I somehow need to execute code on the click event (and same for clear).
I have read a little about not being able to pass event handlers outside of the class it was created, but I'm not really familiar with this.
Is there/and if so what is the best way to achieve my results? Is it possible to handle a click event outside of the class to pass information to other controls?
Any help appreciated. Thanks
Thanks to the comments from Hans Passant for putting me on the right track, I was able to refer to this site Raising Events and solve my issue.

Exposing a Custom Control to Coded UI Tests - WinForms

I wrote my own custom control in WinForms. It's actually works like a TextBox, but have other methodes and extra properties that I wrote.
I tried writing a very simple coded UI test for this control but faild. Every Time I select it with the Coded UI Test Builder it is shown as "Client" and not as a textbox. I cannot read or write to the text property of this control, or get or set other properties.
Do you know how do I expose my custom control for testing, and getting and setting all of it properties?
It is difficult to answer the question without looking at your implementation of the custom control but you can use the following link to learn how to extend a treeview control to make it code UI enabled.
http://www.ranorex.com/blog/enabling-automation-by-adding-accessibility-to-windows-forms-controls
I used that example to learn how to extend my own controls.

RaiseEvent from a UserControl that's placed on a UserControl that's on a Form

I have a Windows Form that contains a custom control container as a UserControl. For the sake of this question, this custom control container is called Dashboard. This container called Dashboard contains numerous other controls depending on their permissions. I need to raise events that are contained on these controls through the Dashboard control and over to the Windows Form.
How can I bubble up the event? I'm using VB.NET for this project, but can convert C# into VB.NET.
Also, to complicate matters, the main Windows Form is a VB6 project. So, I'm using the InteropFormsToolkit to accomplish this.
I figured it out. I just did what I had said that I wanted to do and created an event with a custom eventargs class and bubbled it up to the VB6 app. Each control needed to implement the custom event such as:
Public Event OnMyCustomEvent(source As Object, e As MyCustomEventArgs)
And continue raising that event up to VB6. In VB6 that event was exposed as a function for me to handle as necessary.

Can I change properties of inherited controls at design time?

I am using visual inheritance and was wondering if there is a way to change the properties of inherited controls at design time, preferably in the form designer. If not, then in the designer code.
I have my control declared as Public in the base class. I can access it in the child form code, but not in the form designer. Is this just not possible?
There are limitations placed within visual studio for visual inheritance. Unfortunately, derived forms\usercontrols cannot modify controls containing collections within the base, namely DataGridViewRows, ToolStrips, ListViewColumns, etc.
Microsoft Bug Report
There are ways around this in certain situations. Create a protected property in the base class that exposes the exact properties of the control you wish to modify (DataGridView.BackgroundColor, or ListView.Columns).
Your base form should be able access this property to change the components needed. I've done this for ListView.Columns, and DataGridView.rows successfully, however ToolStrip.Items would not work.
It seems to work only for certain controls, but not all and I can't understand why. On my base form I have a TabControl that within it is a ComboBox, a ToolStrip, and a DataGridView. All of them are set to Public, but I can only modify the properties of the ComboBox and not the other two controls.
I have no idea why this is.
You need to change your control visibility. Put the control property Modifiers on public and recompile the project and then you can change properties of the inherited control.