creating complex lookless controls

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



Answer this question

creating complex lookless controls

  • ddlam

    but in this case I want the code behind to be part of the definition of the custom control. As the entire control template can be swapped out I need any code-behind to exist within the custom control class definition.

  • Kerry_Dixon

    Most likely for designer support. I did check as well and they don't cause any automatic error generation. So it's basically a way to tell inheritors/designers what your control template expects. You have to do the checking in your OnApplyTemplate override yourself.

  • 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

    Thanks Mike, I Think thats exactly what I'm looking for. Are there any examples of creating complicated lookless controls out there I think I've figured a lot of it out, but its been tough going.

  • Pojev

    Yeah, thats where I'm doing the checking now. I assume that will fire on a theme or other template change too, so that events can be rehooked. Thanks for the advice again.

  • Atul Kulkarni

    you should be able to hookup

    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.



  • &amp;#40643;&amp;#27859;&amp;#37327; Jeremy Wong

    As far as I can tell (from empirical tests) the use of this attribute doesnt create a compile time or run time error if that template part doesnt exist in the template.

    I'm wondering what these attributes are for then. Designer support Some unimplimented error handling

    -G

  • creating complex lookless controls