Hi,
try the following code
public sealed partial class Page03UC1 : UserControl
{
public Page03UC1()
{
this.InitializeComponent();
}
public static readonly DependencyProperty DisplayMemberPathProperty =
DependencyProperty.RegisterAttached("DisplayMemberPath", typeof(string),
typeof(Page03UC1), new PropertyMetadata(string.Empty));
public static string GetDisplayMemberPath(DependencyObject obj) => obj.GetValue(DisplayMemberPathProperty).ToString();
public static void SetDisplayMemberPath(DependencyObject obj, String value) => obj.SetValue(DisplayMemberPathProperty, value);
}
public class AttProp
{
public static readonly DependencyProperty DataProperty =
DependencyProperty.RegisterAttached("Data", typeof(Page03Data),
typeof(AttProp), new PropertyMetadata(null, OnDataChanged));
public static Page03Data GetData(DependencyObject obj) => obj.GetValue(DataProperty) as Page03Data;
public static void SetData(DependencyObject obj, Page03Data value) => obj.SetValue(DataProperty, value);
private static void OnDataChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var tb = d as TextBlock;
var data = e.NewValue as Page03Data;
if (tb == null || data == null) return;
var prop = data.GetType().GetProperty(tb.Tag.ToString());
tb.Text = prop.GetValue(data).ToString();
}
}