Beispiel #1
0
 private string PropertyTemplateFor(string propertyName)
 {
     if (string.IsNullOrEmpty(propertyName))
     {
         return(string.Empty);
     }
     return(PropertyTemplates.ContainsKey(propertyName) ? PropertyTemplates[propertyName] : string.Empty);
 }
        private void AssociatedObjectDataContextChanged(object sender,
                                                        DependencyPropertyChangedEventArgs e)
        {
            if (container == null)
            {
                return;
            }
            container.Items.Clear();

            var props = e.NewValue.GetType().GetProperties();

            foreach (var prop in props)
            {
                foreach (var attr in prop.GetCustomAttributes())
                {
                    if (!PropertyTemplates.ContainsKey(attr.GetType()))
                    {
                        continue;
                    }

                    var templ = PropertyTemplates[attr.GetType()];

                    var element = Activator.CreateInstance(templ.Item1) as FrameworkElement;

                    var binding = new Binding
                    {
                        Path = new PropertyPath(prop.Name),
                        Mode = BindingMode.TwoWay,
                        UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
                    };

                    BindingOperations.SetBinding(element, templ.Item2, binding);

                    container.Items.Add(element);
                }
            }
        }