private void SelectChameleonTypeHyperlink_Click(object sender, RoutedEventArgs e)
        {
            if (sender == null) return;
            FrameworkElement element = ((Hyperlink)sender).Parent as FrameworkElement;
            ChameleonBox box = element.GetParent<ChameleonBox>();
            if (box == null) return;

            Confirmation confirmation = new Confirmation()
            {
                Title = "Выберите тип значения",
                Content = this.InfoBase
            };
            this.SelectDataTypeDialog.Raise(confirmation, response =>
            {
                if (response.Confirmed)
                {
                    this.OnDataTypeSelected(box, response.Content as Entity);
                }
                else
                {
                    DataGridCell cell = box.GetParent<DataGridCell>();
                    if (cell == null) return;
                    cell.IsEditing = false;
                }
            });
        }
 private static void OnChameleonValueChanged(DependencyObject target, DependencyPropertyChangedEventArgs args)
 {
     if (args.OldValue == args.NewValue) return;
     ChameleonBox box = target as ChameleonBox;
     if (box == null) return;
     box.ChameleonValue = (args.NewValue == null ? null : args.NewValue);
 }
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            FrameworkElement element = container as FrameworkElement;

            if (element == null)
            {
                return(null);
            }

            DataGridCell cell      = element.GetParent <DataGridCell>();
            bool         isEditing = (cell == null || cell.IsEditing);

            ChameleonBox box = item as ChameleonBox;

            if (box == null)
            {
                return(null);
            }

            Type type = box.ChameleonType?.GetCLRType();

            string templateName = isEditing ?
                                  GetEditableTemplateName(type) :
                                  GetReadOnlyTemplateName(type);

            return(element.FindResource(templateName) as DataTemplate);
        }
 private void UserControl_ValueTypeChanged(object sender, DependencyPropertyChangedEventArgs e)
 {
     ChameleonBox box = sender as ChameleonBox;
     if (box == null) return;
     if (box.ContentTemplateSelector == null) return;
     DataTemplate template = box.ContentTemplateSelector.SelectTemplate(box, (DependencyObject)sender);
     box.ContentTemplate = template;
 }
 // ReferenceObject
 private void SelectChameleonValueHyperlink_Click(object sender, RoutedEventArgs e)
 {
     if (sender == null) return;
     FrameworkElement element = ((Hyperlink)sender).Parent as FrameworkElement;
     ChameleonBox box = element.GetParent<ChameleonBox>();
     if (box == null) return;
     OnSelectReferenceObject(box, box.ChameleonType);
 }
 private static void OnChameleonTypeChanged(DependencyObject target, DependencyPropertyChangedEventArgs args)
 {
     if (args.OldValue == args.NewValue) return;
     ChameleonBox box = target as ChameleonBox;
     if (box == null) return;
     box.ChameleonType = (args.NewValue == null ? null : (Entity)args.NewValue);
     box.UserControl_ValueTypeChanged(box, args);
 }
 private void OnReferenceObjectSelected(ChameleonBox target, ReferenceProxy entity)
 {
     target.ChameleonValue = entity;
     target.ChameleonType = entity.Type;
     UpdateSourceObjectValue(target);
     
     DataGridCell cell = target.GetParent<DataGridCell>();
     if (cell == null) return;
     cell.IsEditing = false;
 }
 private void UpdateSourceObjectValue(ChameleonBox box)
 {
     BindingExpression binding = this.GetBindingExpression(ChameleonBox.ChameleonValueProperty);
     if (binding == null) return;
     binding.UpdateSource();
     object item = binding.DataItem;
     if (item != null && item is Persistent)
     {
         ((Persistent)item).Save();
     }
 }
        private void OnDataTypeSelected(ChameleonBox target, Entity type)
        {
            if (type == null || type == Entity.Empty) return;
            if (type.Code > 0)
            {
                OnSelectReferenceObject(target, type);
                return;
            }
            target.ChameleonValue = Entity.GetDefaultValue(type);
            UpdateSourceObjectValue(target);

            DataGridCell cell = target.GetParent<DataGridCell>();
            if (cell == null) return;
            cell.IsEditing = false;
        }
 private void OnSelectReferenceObject(ChameleonBox box, Entity type)
 {
     Confirmation confirmation = new Confirmation() { Title = string.Empty, Content = type };
     this.SelectReferenceObjectDialog.Raise(confirmation, response =>
     {
         if (response.Confirmed)
         {
             this.OnReferenceObjectSelected(box, response.Content as ReferenceProxy);
         }
         else
         {
             DataGridCell cell = box.GetParent<DataGridCell>();
             if (cell == null) return;
             cell.IsEditing = false;
         }
     });
 }
 private static void OnInfoBaseChanged(DependencyObject target, DependencyPropertyChangedEventArgs args)
 {
     ChameleonBox box = target as ChameleonBox;
     if (box == null) return;
     box.InfoBase = (args.NewValue == null ? null : (InfoBase)args.NewValue);
 }