locked
[U8.1][XAML]Bindings and Events: Reusing in XAML

    Question

  • I have a large number of the same UserControl in my XAML, and they all have the same handler for their Tapped event, and they all have the same property bound to themselves. For example, I have 24 UserControls on my Page that all look like the following (except, of course, for the Grid.Column and Grid.Row):
    <ctrl:Card Grid.Column="0" Grid.Row="0" Opacity="{Binding Value,RelativeSource={RelativeSource Mode=Self},Converter={StaticResource HideValueOpacity}}" Tapped="Row_Tapped"/>

    I would like to be able to set the Opacity and Tapped handler to all 24 instances the same way other properties are set using a Style (or something similar), but to the best of my knowledge, there is no way to do this in XAML. Is there a way to do this that is similar to the way a Style is used? I think it would greatly shorten and clean up my code. Any help would be appreciated. Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/


    • Edited by Jamles HezModerator Monday, May 04, 2015 3:17 AM add title tag per: https://social.msdn.microsoft.com/Forums/en-US/b479e984-ca39-4b34-a6b7-85194b54d8bd/guide-to-posting-subject-line-tags
    Sunday, May 03, 2015 1:33 AM

Answers

  • Hi Nathan,

    How about programmatically set them in the class constructor section? Using a for loop and some code can also shorten the xaml code.

    I don't think there is some way to implement this like apply a Style.

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, May 04, 2015 2:35 AM
    Moderator
  • I am fully aware of the fact that I can do it in the codebehind, I was hoping to do it in the XAML so that I could, if desired, add the same handler to all of them at the same time (like a mouseover-type event, for example). I was kind of going for the keeping design and functionality separate concept. But it is good to know that there is no way to do it in XAML so that I don't try to look for something that doesn't exist. Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Monday, May 04, 2015 2:30 PM

All replies

  • Hi Nathan,

    How about programmatically set them in the class constructor section? Using a for loop and some code can also shorten the xaml code.

    I don't think there is some way to implement this like apply a Style.

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, May 04, 2015 2:35 AM
    Moderator
  • I am fully aware of the fact that I can do it in the codebehind, I was hoping to do it in the XAML so that I could, if desired, add the same handler to all of them at the same time (like a mouseover-type event, for example). I was kind of going for the keeping design and functionality separate concept. But it is good to know that there is no way to do it in XAML so that I don't try to look for something that doesn't exist. Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Monday, May 04, 2015 2:30 PM