private static StringFormatDataTemplate Create(DataTemplate contentTemplate, string format, CultureInfo culture)
        {
            // We are using a converter to format the target value instead of using the
            // ContentPresenter.ContentStringFormat or Binding.StringFormat property because
            // it is not applied if a ContentPresenter.ContentTemplate is given or if the binding is
            // not targeting a string property.
            var template = new StringFormatDataTemplate(contentTemplate);
            var control  = new FrameworkElementFactory(typeof(ContentPresenter));

            var binding = new Binding();

            binding.Path               = new PropertyPath(FrameworkElement.DataContextProperty);
            binding.RelativeSource     = RelativeSource.TemplatedParent;
            binding.Converter          = s_converter;
            binding.ConverterParameter = format;
            binding.ConverterCulture   = culture;

            control.SetValue(ContentPresenter.ContentTemplateProperty, contentTemplate);
            control.SetValue(ContentPresenter.ContentProperty, binding);

            template.VisualTree = control;
            template.Seal();

            return(template);
        }
Пример #2
0
        internal override DataTemplate GetCellStringFormatDataTemplate(DataTemplate contentTemplate)
        {
            // parentColumn is verified to be not null in the calling method
            var parentColumn = this.ParentColumn;

            var format = parentColumn.CellContentStringFormat;

            Debug.Assert(!string.IsNullOrEmpty(format));

            return(StringFormatDataTemplate.Get(contentTemplate, format, parentColumn.GetCulture()));
        }
Пример #3
0
        internal override DataTemplate GetCellStringFormatDataTemplate(DataTemplate contentTemplate)
        {
            var parentColumn = this.ParentColumn;

            Debug.Assert(parentColumn != null);

            var format = parentColumn.CellContentStringFormat;

            Debug.Assert(!string.IsNullOrEmpty(format));

            return(StringFormatDataTemplate.Get(contentTemplate, format, parentColumn.GetCulture()));
        }
            internal Entry(StringFormatDataTemplate template, string format, CultureInfo culture)
            {
                Debug.Assert(format != null);

                if (template != null)
                {
                    m_template = new WeakReference(template);
                }

                m_format  = format;
                m_culture = culture;
            }
        internal static StringFormatDataTemplate Get(DataTemplate contentTemplate, string format, CultureInfo culture)
        {
            if (string.IsNullOrEmpty(format))
            {
                throw new ArgumentException("The format must be non empty.", "format");
            }

            lock ((( ICollection )s_templates).SyncRoot)
            {
                int  insertionIndex = 0;
                bool cleanUp        = false;
                StringFormatDataTemplate template = null;

                if (s_templates.Count > 0)
                {
                    var lookup = new Entry(format, culture);
                    insertionIndex = s_templates.BinarySearch(lookup, s_comparer);

                    if (insertionIndex >= 0)
                    {
                        for (int i = insertionIndex; i < s_templates.Count; i++)
                        {
                            var item = s_templates[i];
                            if (s_comparer.Compare(lookup, item) != 0)
                            {
                                break;
                            }

                            var target = item.Template;
                            if (target == null)
                            {
                                cleanUp = true;
                            }
                            else
                            {
                                Debug.Assert(object.Equals(item.Format, format) && object.Equals(item.Culture, culture));

                                if (target.m_contentTemplate == contentTemplate)
                                {
                                    template = target;
                                    break;
                                }
                            }
                        }
                    }
                    else
                    {
                        insertionIndex = ~insertionIndex;
                        cleanUp        = (insertionIndex < s_templates.Count) && (s_templates[insertionIndex].Template == null);
                    }
                }

                if (template == null)
                {
                    template = StringFormatDataTemplate.Create(contentTemplate, format, culture);
                    s_templates.Insert(insertionIndex, new Entry(template, format, culture));
                }

                if (cleanUp)
                {
                    StringFormatDataTemplate.CleanUpCache();
                }

                Debug.Assert(template != null);

                return(template);
            }
        }