Giving event control back to xaml/callee to access check/uncheck

Posted by dovholuk on Stack Overflow See other posts from Stack Overflow or by dovholuk
Published on 2010-05-03T12:19:32Z Indexed on 2010/05/10 5:28 UTC
Read the original article Hit count: 335

Filed under:
|

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}}" 
/>

© Stack Overflow or respective owner

Related posts about Silverlight

Related posts about customcontrols