locked
User Control with Dynamic Content RRS feed

  • Question

  • I am trying to create a user control made up of an image and two text blocks. I want to be able to drop this user control into a project and change the image source and the text for the text blocks. How would I go about doing this strictly in XAML (no code-behind)?

    Thanks,
    Todd
    • Edited by Todd Pierpoint Friday, February 29, 2008 10:17 PM changed verbiage
    Friday, February 29, 2008 10:15 PM

All replies

  • Hi Todd,

    You should expose properties in your UserControl that represent the Image (of type ImageSource) and the two text blocks (hence of type String). You can then set these anywhere where you instantiate the UserControl.

    In your UserControl xaml, you can then utilize these properties like:

    <UserControl....
    xmlns:...
    x:Name="UserControl"
    <Grid>
    <TextBlock Text="{Binding ElementName="UserControl", Path=MyStringProperty}" />
    </Grid>
    >

    My apologies if I messed up the XAML, but you can author the XAML in Blend :).
    Friday, February 29, 2008 11:58 PM
    Moderator
  • Hi, Unni. Please pardon my ignorance, but would you please give me an example of how to expose properties for images and text blocks in a user control?

    Thanks,

    Todd

    Tuesday, March 11, 2008 6:51 PM
  • Hi Todd,

    Here is an example: http://blogs.msdn.com/expression/articles/525441.aspx

    Thanks,
    -Unni
    Tuesday, March 11, 2008 7:04 PM
    Moderator