⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 itemscontroluiadapter.cs

📁 wpf和cab的结合使用源码(转载)
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using Microsoft.Practices.CompositeUI.UIElements;
using Microsoft.Practices.CompositeUI.Utility;

namespace Microsoft.Practices.CompositeUI.Windows.UIElements
{
	/// <summary>
	/// Implements a <see cref="UIElementAdapter"/> for <see cref="FrameworkElement"/>s.
	/// </summary>
	public class ItemsControlUIAdapter : UIElementAdapter<FrameworkElement>
	{
		private readonly ItemsControl _itemsControl;

		public ItemsControlUIAdapter(ItemsControl itemsControl)
		{
			Guard.ArgumentNotNull(itemsControl, "itemsControl");
			_itemsControl = itemsControl;
		}

		protected override FrameworkElement Add(FrameworkElement uiElement)
		{
			_itemsControl.Items.Insert(GetInsertingIndex(uiElement), uiElement);
			return uiElement;
		}

		protected override void Remove(FrameworkElement uiElement)
		{
			//wpf gets confused if the application is gone
			if (Application.Current != null)
			{
				_itemsControl.Items.Remove(uiElement);
			}
		}

		protected virtual int GetInsertingIndex(FrameworkElement uiElement)
		{
			int mergeIndex = 0;

			//a dirty hack to allow menu items to be placed in specific positions amongst
			//other menu items. i'm heading to confession on Sunday
			//TODO: change this to an attached property?

			if (uiElement.Tag is int)
			{
				mergeIndex = (int) uiElement.Tag;
			}
			else
			{
				return _itemsControl.Items.Count;
			}

			int index = 0;

			for (int i = 0; i < _itemsControl.Items.Count; ++i)
			{
				FrameworkElement frameworkElement = _itemsControl.Items[i] as FrameworkElement;

				if (frameworkElement != null)
				{
					int itemMergeIndex = 0;

					if (frameworkElement.Tag is int)
					{
						itemMergeIndex = (int) frameworkElement.Tag;
					}

					if (mergeIndex > itemMergeIndex)
					{
						index = i + 1;
					}
					else
					{
						break;
					}
				}
			}

			return index;
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -