Set RelativeSource CommandTarget in RoutedCommand.Execute in code behind - wpf

I have a static class called commands. One the RoutedCommands in it is called ConfirmNoPrint. I want to Execute it in code behind from my custom control like this:
Commands.ConfirmNoPrint.Execute(null, [WHAT_DO_I_PUT_HERE]);
In the custom control class I have an instance of Binding whose RelativeSource property is set like this:
_mainControlBinding.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(UserControl), 2);
Can I use one of the properties of _mainControlBinding to get the instance of IInputElement I need to pass as the second parameter of Commands.ConfirmNoPrint.Execute ?
The command binding for ConfirmNoPrint is the parent of my custom control, but it is in a different assembly. I can't add a reference to it since it would cause a circular reference.
I am barking up the wrong tree entirely?

Routed commands are, by definition, routed. If I understand your problem well, you just have to pass this as the second parameter of your command (assuming you're into the control class). The command will be bubbling up the visual tree until it encounters the command binding on the parent.

Related

Bind an XAML event to class other than file root

Windows Phone 8 project, XAML. I have a ListBox inside a pivot item that's of my own class MyPivotItem (derived from vanilla PivotItem) inside a page. The listbox has an ItemTemplate with some controls. I'd like to bind an event in one of those controls to a method in MyPivotItem. The plain syntax Click="OnClick" does not work - the frameword searches for the method in the Page class only.
I could derive the control itself and do some trickery with tree navigation and event forwarding and so on, but I wonder if such a scenario can be served by XAML's internal means.
Is there any way to bind methods non-programmatically to a class that's deeper in the hierarchy than the root object of the XAML file?
Without using something like behaviors or attached properties, it is not possible. XAML file is always associated with partial C# class and in this class you define your controls and events. For every Page.xaml, a Page.g.xaml file is generated that actually creates controls and binds events. However, those events must be defined in root object - the partial class itself.
Even if you create a workaround using either attached properties or behaviors, you are simply offloading that programmatic code to some other place and hiding it behind XAML syntax.
When would you want to bind to control's specific event handlers anyway? Can you simply use UserControls for that?

Add DependencyObject to control from code-behind

I want to add a DependencyObject to a control from code behind. I have searched and searched online for how to do this with absolutely no success. The DependencyObject has a DependencyProperty. I also want to set this property from code-behind.
charting:ChartBehaviors inherits directly from DependencyObject. It is a class I wrote myself. The Chart control is a Third-party control.
charting:ChartBehaviors.FloatingTooltip is the DependencyProperty. This is also a class I wrote myself.
Here is what it looks like in XAML. I want to do this in code behind so that I can turn on and off the "behavior".
<charting:Chart>
<charting:ChartBehaviors.FloatingTooltip>
<charting:FloatingTooltipBehavior
TooltipTemplate="{StaticResource tooltipTemplate}" />
</charting:ChartBehaviors.FloatingTooltip>
</charting:Chart>
You can just use the SetValue method on the object (assuming you give your chart an id of chart1).
var behave = new FloatingTooltipBehavior();
chart1.SetValue(ChartBehaviors.FloatingTooltipProperty, behave);
Another solution would be to just add an Enabled property to your behavior and then set that from the code behind.
Well, you basically instantiate necessary dependency object and use appropriate method to add it to the control. For example, to add TextBlock to the StackPanel, you write it like this:
TextBlock txtMyText = new TextBlock();
stackPanel.Children.Add(txtMyText);
If you're wanting to add certain behavior to a chart, you should just get your chart object in code and look for a property like Behaviors or something. Then you either assign a behavior (if it's one-behavior-only) or add it like to the stackpanel:
Chart myChart;
myChart.Behavior = new FloatingTooltipBehavior();
It's hard to tell the exact syntax without knowing the component.

need to create binding and pass parameter in code behind

I need to pass a parameter to a binded command of a menuitem.click
the menu items are created dynamically in code behind, how can i create binding and a commandparameter in code behind?
Here is the answer: Binding Declarations Overview.
Basically you create an instance of Binding type and then assign it to an object via BindingOperations.SetBinding() method.

Change data context when using UserControls

I'm using a simple main window that contains 2 custom User Controls (that i've created)
those user controls has ModelView code file for each (im using MVVM pattern).
each ModelView file contains single Command (and command implementation of Execute and CanExecute).
The problem is that when i need to active each command (through the MainWindow cause the main window holds those custom user controls) i need to change the DataContext
of the Main window to the ViewModel of the control that is currently in focus otherwisw i cant execute the command (the Command binding inside the UserControl.xaml cant find the Command).
I think that tracking after focused UserControl in order to change the MainWindow DataContext is not the way.
Is anyone faced this kind of problem before ??
Thanks.
A way of solving this is by creating a ViewModel for the main window and add two properties to this VM, one for each ViewModel you created before.
This way you can assign the new VM to the DataContext of the Window and Bind the DataContext of each user control to one of the properties.
Put the commands on the correct VM where you want to use it.
Does this make sense?
BTW: you call ModelView what I'd call ViewModel.

Automatically creating child objects during Silverlight DataBinding?

I have a parent object of type Parent and it currently has a null property called Foo of type Child and that Child class has a property of type string called Name.
If the user types into a Text Box for that Name property then I want to automatically create an instance of Child and set it as the Foo property of Parent before finally setting the Name property of the Child object.
If i use:
{Binding parent.foo.name, Mode=TwoWay}
It doesn't create foo and essentially does nothing. Is there any way to achieve what I want without pre-creating all the possible child objects and then removing them if properties haven't been set?
There is no automatic way. You could consider using a pattern like M-V-VM and handling this logic in the ViewModel. You might also possibly get creative with an IValueConverter so that your binding can run custom code when the value is set. But WPF / Silverlight binding will not automatically do this work for you.

Resources