コード例 #1
0
        /// <summary>
        /// Convert a TreeViewItem into a value based on the depth of the item
        /// in the TreeView.
        /// </summary>
        /// <param name="value">The TreeViewItem.</param>
        /// <param name="targetType">
        /// The indentation type to convert to (such as Thickness or double).
        /// </param>
        /// <param name="parameter">
        /// The number of pixels to indent each level of the TreeView.  A
        /// default value of 15.0 will be used if no parameter is provided.
        /// </param>
        /// <param name="culture">
        /// The culture used to convert the TreeViewItem.
        /// </param>
        /// <returns>
        /// A value based on the depth of the item in the TreeView.
        /// </returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            TreeViewItem item = value as TreeViewItem;

            if (item != null)
            {
                // Determine the number of pixels to indent each level of the
                // TreeView (or use the default value of 15.0)
                double indentationPerDepth          = 15.0;
                string indentationPerDepthParameter = parameter as string;
                if (string.IsNullOrEmpty(indentationPerDepthParameter) ||
                    double.TryParse(indentationPerDepthParameter, NumberStyles.Any, culture, out indentationPerDepth))
                {
                    try
                    {
                        // Convert the depth into the indentation
                        double indentation = item.GetDepth() * indentationPerDepth;
                        return(WrapIndentation(indentation, targetType));
                    }
                    catch (ArgumentException)
                    {
                        // Ignore the case where a TreeViewItem isn't associated
                        // with a TreeViewItem, in which case we'll return an
                        // indentation of zero anyway
                    }
                }
            }

            return(WrapIndentation(0, targetType));
        }