Bind an XAML event to class other than file root - wpf

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?

Related

WPF Prism Unity Container. Setting keyboard focus on usercontrol or usercontrol grid parent

I'm working on a project that utilizes WPF, using the Prism library and Unity Container.
I have a mainwindow, which contains a mainviewmodel control which in turn is populated by a collection of user controls.
My goal is to set keyboard focus to the mainviewmodel where I have an EventTrigger InvokeCommandAction which reacts to keyeventsargs...
Currently the only way the command is fired if I use a textbox within the control (or child controls). My goal is to have the mainviewmodel control or grid get and preserve keyboard focus.
Any tips would be appreciated!
//Nathan
Either not understanding your question correctly or you should review the basic concepts of MVVM in a WPF implementation.
The View is the WPF controls.
WPF Window/UserControl files contain WPF markup which is the View.
Controls in a view leverage DataBindings to the DataContext property of either the control itself or the parent containing control (which it will inherit).
DataContext property is set to an instance of an object that is the ViewModel. It contains properties to hold values and commands to execute actions.
So conceptually there is no "mainviewmodel control", there is a MainView which contains controls and may in this case have its DataContext property set to an instance o MainViewModel. (hence my confusion)
Finally, while it is possible and some might even recommend writing UI rules/logic in a view model I haven't found much benefit in it.
You are much better off putting UI logic in the XAML or in the MinView code behind. You can still access the MainViewModel in the code behind by casting the MainView.DataContext property as a MainViewModel.
So for example:
MainView.KeyDown event can be wired up to call MainViewModel.CommandX.Execute();

When are the elements of a ContentProperty actually created?

I am creating a custom WPF control that uses the following markup:
<custom:FilterPanel
Grid.Row="1"
FilterTarget="{Binding Path=MyItems}">
<custom:FilterParameter
ParameterName="Name"
TargetProperty="Name" />
<custom:FilterParameter
ParameterName="Date"
TargetProperty="MyDate" />
</custom:FilterPanel>
I've set the ContentProperty for my FilterPanel to FilterParameters, which is obviously a collection of FilterParameter objects that I add items to using the markup above. My question is, when are the elements of a ContentProperty actually processed so that instances are created and items are actually added to the underlying collection?
I'm interested in sharing the data source of the parent control with its children, is there any point in the WPF lifecycle where I can override this behavior and add custom logic to the creation of this collection of FilterParameters?
Assuming your class derives from Panel, then the first point in the Panel's lifecycle where you can see children (i.e. children that are decalred in XAML like your example - not children generated via bindings) is Panel.EndInit(), a virtual method you can override in your derived class. Specifically the chldren created between the BeginInit and EndInit methods.

Silverlight: How to bind to a resource that is defined as a usercontrol resource in code

I have a string value that i have defined as a resource in a user controls' constructor
public void usercontrolClas()
{
this.Resources.Add("stringState", "foo");
}
I am trying to access this string as a StaticResource in the XAML of a child usercontrol of the above usercontrol.
<Button Content={.... ,ConverterParameter={StaticResource stringState}"/>
However this doesnt work. Its working if i add the resource to Application.Resources, but I have some state thats is instance specific to the usercontrol and hence has to be scoped to the usercontrol. Anyde
{StaticResource} won't work here. Once you bind it using {StaticResource} changes in resource won't be reflected. And in your case, the resource does not even exist until you create it after UserControl's InitializeComponent() method.
To make this work, you will need {DynamicResource}. But, unfortunately, it is not available in WP7 Silverlight right now.
Other way you could go is creating an Attached Property or a Custom Behavior to work around this limitation.
Tutorials on Behaviors:
Silverlight Behaviors and Triggers
Using Behaviors to enable MouseWheel in Full Screen and Out-of-browser
Silverlight 3 Drag Behavior

WPF Events - How to map the XAML to an event on the base class?

I am using a base class to my Popup windows on a WPF application. Everything looks and works great beside one thing : I cant map an event handler that is on the base class to the xaml.
Ofcourse i can easily have the event on the implemented class, and use the method to call the base class, but i just wanted to know if there a way to have the code cleaner while implementing all my generic event handlers in one place and mapping the xamls to them.
Example code :
<CommandBinding Command="Save" Executed="Save_Executed" CanExecute="Save_CanExecute" />
And have Save_Executed handler on the base class of 'MyClass.cs'.
Thanks,
Oran
Solved my issue through a different approach...
No need to use xaml mapping. i just added my base class a routine to register button events through code :
CommandBinding saveBinding = new CommandBinding(ApplicationCommands.Save, Save_Executed, Save_CanExecute);
_saveButton.CommandBindings.Add(saveBinding);
Ofcourse that _saveButton is the actual implmemented popup window button that is passed to the base class on initialization.
Oran

Scope of controls inside Silverlight XAML

I have defined a textbox with x:Name="txtMyTextBox" inside UserControl called MyView. I've noticed that I can do the following:
MyView myView = new MyView();
myView.txtMyTextBox.Text = "something";
Why txtMyTextBox is accessible that way? Is it public or internal field? Can I make it private?
The Silverlight XAML designer creates fields for named elements so that you can access them from the code behind. You can see the generated file if you go into the code behind and choose InitializeComponent from the method selection drop down at the top. It's kept in a partial file. In the past, designer generated fields have been scoped as private, but for some reason I cannot fathom, the current crop of XAML designers (VS2010, Blend) creates it as internal.
You can change the visibility of the field that gets generated by using the x:FieldModifier attribute but you probably don't need to worry about it. If you need to, you should expose a public property from your user control that wraps access to it instead.

Resources