locked
How to bind a command in ItemsControl DataTemplate defined in a templated control RRS feed

  • Question

  • Hi,

    I'm writing a template control for my app. In the control template I have an ItemsControl and its ItemsSource is bound to an Items dependency property defined in the templated control.

    Inside the DataTemplate of the ItemsControl I have a button and I would like to attach to the Click event or bind a command defined in the templated control.

    <Style TargetType="local:TestCustomControl">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="local:TestCustomControl">
                        <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
                            <ItemsControl ItemsSource="{TemplateBinding Items}">
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <!-- How to attach to click event or bind a to a templated control command? -->
                                        <Button Content="{Binding Text}" />
                                    </DataTemplate>                                
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

    How can I do this?

    Sunday, January 12, 2014 5:14 AM

Answers

  • Yes it works now, but I think it's only a workaround.

    I think the best solution is to
     define commands at control level instead of having commands for each itemscontrol item and then bind them with AncestorType and AncestorLevel RelativeSource binding (like Silverlight 5). Unfortunately we don't have this feature right now in Windows Store apps.
    I think it's very important especially when using MVVM and binding commands.

    Will this feature be available with Windows 8.1 Update 1?

    Saturday, January 18, 2014 4:55 AM

All replies

  • Does simply setting Click="eventhandler" not work?


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Tuesday, January 14, 2014 3:55 PM
    Moderator
  • Setting Click="eventhandler" doesn't work.

    The only way I found is to add a property of type ICommand to the object bound to the ItemsControl item and then bind button Command.

    Wednesday, January 15, 2014 4:28 AM
  • So, is this problem resolved?

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Friday, January 17, 2014 1:35 PM
    Moderator
  • Yes it works now, but I think it's only a workaround.

    I think the best solution is to
     define commands at control level instead of having commands for each itemscontrol item and then bind them with AncestorType and AncestorLevel RelativeSource binding (like Silverlight 5). Unfortunately we don't have this feature right now in Windows Store apps.
    I think it's very important especially when using MVVM and binding commands.

    Will this feature be available with Windows 8.1 Update 1?

    Saturday, January 18, 2014 4:55 AM
  • There are no comments on unreleased features. :-)

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Tuesday, January 21, 2014 2:55 PM
    Moderator