Hey,
What would be the best practice for collecting events from the template controls for a lookless custom control. Say I have 3 text areas on the custom control whose text is bound to some properties. That part is easy enough to do, but say I want to know if any of these areas have lost focus, and which one lost the focus
Since these controls that are holding the text are defined in the template I don't have an easy way of discriminating between them. I can handle previewlostfocus or whichever, but to determine which was the control that lost fous means to me I'm left with the rather unsatisfactory solution of relying that they have some identifier in their control name, or something of that nature.
Is there any way of rerouting an event on a control in a template to some delegate in the custom control definition
Any ideas
-G

creating complex lookless controls
ddlam
Kerry_Dixon
Julien Couvreur
I dont know if it will help your situation, instead of using textbox you might be able to use FrameworkElement.lostfocus or soimething like that
PublicError
I've tried to make my PowerGrid as lookless as possible...although it really wouldn't make sense being much more than a grid. Go to http://www.codeplex.com/Wiki/View.aspx ProjectName=wpftoolbelt to download the source and see what I mean. I don't create any visual elements within the control.
Also just use Reflector to look at some of the controls that come with the library.
John Dekker
Pojev
Atul Kulkarni
TextBox.LostFocus
="txt_LostFocus" on the panel holding those TextBoxes, something like this should work<
StackPanel Name="sp1" TextBox.LostFocus="txt_LostFocus"><
TextBox Text="text1" Name="txt1"></TextBox><
TextBox Text="text2" Name="txt2"></TextBox></
StackPanel>
void txt_LostFocus(object sender, RoutedEventArgs e){ MessageBox.Show((e.OriginalSource as TextBox).Name);}
globelin
Use TemplateParts.
Give each of the child controls that you need to be present in your control's template a name in your template like PART_TextControl1.
Add a TemplatePartAttribute to your control that specifies the name of the required control and its type.
Now in your constructor add a handler to your control's Loaded event that gets a handle to your control parts and assigns handlers to their LostFocus events.
[TemplatePart(Name="PART_TextControl1" Type=typeof(TextBox))]
public class MyControl: Control
{
public MyControl()
{
this.Loaded+=new RoutedEventHandler(OnLoaded);
}
private void OnLoaded(Object Source, RoutedEventArgs e)
{
TextBox TextControl1=Template.FindName("PART_TextControl1") as TextBox;
if(TextControl1!=null)
{
TextControl1.LostFocus+=new RoutedEventHandler(TextControl1_LostFocus);
}
}
protected virtual void TextControl1_LostFocus(Object sender, RoutedEventArgs e)
{
//Perform whatever actions you want here e.OriginalSource is your TextBox
}
}
By placing the TemplatePartAttribute on your class, you are basically saying that part of the code contract is that the template used for this control must have an element named PART_TextControl1 and it must be of typ TextBox. I believe that the WPF runtime will throw an exception saying as much for you but if not, throw one within your OnLoaded event.
&#40643;&#27859;&#37327; Jeremy Wong
I'm wondering what these attributes are for then. Designer support Some unimplimented error handling
-G