Answered by:
reuse objects, data, styles

Question
-
in my app I have many pages which are pretty similar. They all bind to different instances of the same datamodel and show data in the same manner.
I have a "contact card" which is common to multiple pages. At the moment this is just populated via
<textblock text={Binding ElementName=listNames, Path=SelectedItem.GivenName}>
<textblock text={Binding ElementName=listNames, Path=SelectedItem.sn}> etc etc etc
I would like to take this "contact card" and make it into a control, or datatemplate, but I don't know which one. The "contact card" only displays data and does not modify it. Certain text items will display differently based on the value of the text. My goal is that I can just update the object in one location and then that is then called by the other pages, so my code is in one location.
if I use a usercontrol, how do I pass the selectecitem itemsource from my page to the control, so that the bindings are correctly used?
many thanks - Im coming from winforms and xaml has a steep learning curve.
- Moved by Rob Caplan [MSFT]Microsoft employee, Moderator Wednesday, October 30, 2013 3:59 PM coding question, not a design question
Wednesday, October 30, 2013 3:11 PM
Answers
-
ok so I've decided to use a datatemplate and content control, this can be closed. if anyone wants a simple example let me know
- Marked as answer by I_windows Thursday, October 31, 2013 7:16 AM
Thursday, October 31, 2013 7:16 AM -
Hi,
You can register a dependency property in a UserControl and make the TextBlock control Text property bind to the UserControl’s dependency property. Then, you can set the different UserControl dependency property value in different page. That make you can reuse the UserControl and pass the selectecitem value from my page to the UserControl.
See some codes:
Define a dependency property in UserControl:
public partial class MyUserControl1 : UserControl { public string Text { get { return (string)GetValue(TextProperty); } set { SetValue(TextProperty, value); } } // Using a DependencyProperty as the backing store for Text. This enables animation, styling, binding, etc... public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(MyUserControl1), new PropertyMetadata(string.Empty)); }
Define a UserControl in XAML and set the dependency property bind to TextBlock Text property:
<UserControl ... x:Name="self"> ... <TextBlock Text="{ Binding ElementName=self, Path=Text }" /> ... </UserControl>
See Dependency properties overview
Best Wishes!
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. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.Thursday, October 31, 2013 7:36 AM
All replies
-
ok so I've decided to use a datatemplate and content control, this can be closed. if anyone wants a simple example let me know
- Marked as answer by I_windows Thursday, October 31, 2013 7:16 AM
Thursday, October 31, 2013 7:16 AM -
Hi,
You can register a dependency property in a UserControl and make the TextBlock control Text property bind to the UserControl’s dependency property. Then, you can set the different UserControl dependency property value in different page. That make you can reuse the UserControl and pass the selectecitem value from my page to the UserControl.
See some codes:
Define a dependency property in UserControl:
public partial class MyUserControl1 : UserControl { public string Text { get { return (string)GetValue(TextProperty); } set { SetValue(TextProperty, value); } } // Using a DependencyProperty as the backing store for Text. This enables animation, styling, binding, etc... public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(MyUserControl1), new PropertyMetadata(string.Empty)); }
Define a UserControl in XAML and set the dependency property bind to TextBlock Text property:
<UserControl ... x:Name="self"> ... <TextBlock Text="{ Binding ElementName=self, Path=Text }" /> ... </UserControl>
See Dependency properties overview
Best Wishes!
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. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.Thursday, October 31, 2013 7:36 AM