views:

136

answers:

2

Hi All,

I've scoured the Internet but my search-foo must not work if it's out there cause I just can't find the proper search terms to get me the answer I am looking for. So I turn to the experts here as a last resort to point me in the right direction.

What I'm trying to do is create a composite control of a text box and a list box but I want to allow the control consumer to decide what to do when say the checkbox is checked/unchecked and I just can't figure it out... Maybe i'm going about it all wrong.

What I've done thus far is to:

  1. create a custom control that extends ListBox
  2. expose a custom DP named "Text" (for the Text box but it's not the important part)
  3. craft the generic.xaml so that the list items have a default ItemTemplate/DataTemplate
    1. inside the DataTemplate I'm trying to set the "Checked" or "Unchecked" events
  4. expose 'wrapper' events as DPs in the custom control that would get 'set' via the template when instatiated

As soon as I try something like the following (inside generic.xaml):

<DataTemplate>
    <...>
    <CheckBox Checked="{TemplateBinding MyCheckedDP}"/>
    <...>
</DataTemplate>

I get runtime exceptions, the designer - vs2010 - pukes out a LONG list of errors that are all very similar and nothing I do can make it work.

I went so far as to try using the VisualTreeHelper but no magic combination I could find would work nor would it allow me to traverse the tree because when the OnApplyTemplate method fires the listbox items don't exist yet and aren't in the tree.

So hopefully this all makes sense but if not please let me know and I'll edit the post for clarifications.

Thanks to all for any pointers or thoughts... Like I said maybe I'm heading about it in the wrong way from the start...

EDIT (Request for xaml)

generic.xaml datatemplate:

<DataTemplate >
    <StackPanel Orientation="Horizontal" >
        <local:FilterCheckbox x:Name="chk">
            <TextBlock Text="{Binding Path=Display}" />
        </local:FilterCheckbox>
    </StackPanel>
</DataTemplate>

usercontrol.xaml (invocation of custom control)

<local:MyControl FancyName="This is the fancy name"
    ItemChecked="DoThisWhenACheckboxIsChecked" <-- this is where the consumer "ties" to the checkbox events
    ItemsSource="{Binding Source={StaticResource someDataSource}}" 
/>
A: 

Hi,

Assuiming that you want to fire the event from your Custom Control, while clicking on the checkBox , and handling it in your implementing class, the following should work.

First of all try to remove the template binding with event, though we expect it may work as it works templateBinding DependencyProperty

So your XAML will look something as below:

<DataTemplate> 
    <...> 
    <CheckBox x:Name="myCheckBox" /> 
    <...> 
</DataTemplate>

Now You have to access your checkBox from generic.cs as below:

 public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
           CheckBox myCheckBox= GetTemplateChild("myCheckBox") as CheckBox;
         }

Now You just need to add a event to your Control.

public event EventHandler checkBoxChecked;

Now this are typically implemented like this:-

 protected virtual void onCheckBoxChecked(EventArgs e) 
    { 
         var handler = checkBoxChecked;
        if (handler != null) 
             handler(this, e);    
     }

Now in your existing checked events:-

myCheckBox.Checked+= (obj, Args) => 
{
    onCheckBoxChecked(EventArgs.Empty); 
}

Now in your consuming code or Implementing Application you can do this sort of thing(Assuming your CustomControl Name is myCustomControl):-

myCustomControl.checkBoxChecked+=(s, args) => { /* Your Code Here*/ }; 

Hope this is what you were trying to acomlish. If need anything esle , letz discuss here.

Update

Well there is detailed discussion about onApplyTemplate and loading the controls and accessing the events outside that class:

Have a look:

http://stackoverflow.com/questions/2690177/how-to-access-a-button-present-inside-a-custom-control-from-the-implementing-pag

Subhen
ahhh - the beauty of a bounty... sadly your solution does not work for a few reasons... first there are "n" check boxes so getting only 'one' wouldn't work right anyway... i need to recurse through all checkboxes and tie the check/uncheck to the 'provided' method by the callee. the reason why this won't work (i tried using VisualTreeHelper to do what you're doing btw) is that the controls are not instantiated when OnApplyTemplate is fired. so i appreciate the effort but it doesn't work. do keep trying though! :)
dovholuk
Could you ShowUp your XAML here.
Subhen
"which" xaml? the totality of it is verbose (as xaml can be)... the generic.xaml is 70 lines, the user control is another 60 lines... I'll try to put in an edit "the key pieces" of information...
dovholuk
A: 

Well I dislike answering my own question but the answer above doesn't work for me nor does the link referenced do what i'm looking for.

I'm posting this only answer so that i can maintain the 100% answer rate...

dovholuk