SketchFlow下拉值 [英] SketchFlow Drop-down Values

查看:70
本文介绍了SketchFlow下拉值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常需要添加一个下拉列表,其中包含在一个范围内递增一(1)的值。例如,1990年到2010年之间的所有年份的下拉列表。

I frequently need to add a drop-down that contains values that increment by one (1) within a range. For example, a drop-down covering all years between 1990 and 2010.

除了手动添加一堆ComboBoxItems之外,还有更简单的方法吗?



我猜一种比通过Items集合添加它们更容易的方法是编辑XAML,但由于这最终将由DB中的代码或值驱动,为什么要在这里手动执行?

Is there an easier way to do this than manually adding a bunch of ComboBoxItems?

I guess one way that's easier than adding them via the Items collection is to edit the XAML, but since this will ultimately be driven by code or values in a DB why do it by hand here?

"我们都在一起,孩子。" - Harry Tuttle,加热工程师

"We're all in it together, kid." - Harry Tuttle, Heating Engineer

推荐答案

这是行为的一个很好的用例,如果你不熟悉,行为是一些代码可以拖放到画板上的控件上,并且可以轻松重复使用。 

This is a good use case for a behavior, if you aren't familiar, a behavior is a bit of code that can be dragged/dropped onto controls on the artboard, and is easily re-usable. 

要使用此代码:


  1. 在你的项目中,转到文件 - >新项目,选择行为,将其命名为"AddRangeOfValuesBehavior"。
  2. 将下面的代码粘贴到新文件中。
  3. 构建
  4. 从资产中工具/窗格,找到您的新行为并将其拖动到组合框中(它也可以与ListBox / ItemsControl一起使用)。
  5. 在"对象和时间线"窗格中选择行为,设置最小值/最大值/增量值属性窗格。
  6. 运行,查看结果。

此类代码不会在设计界面上运行,仅在运行时运行,仅供参考。

This type of code won't run on the design surface, only at runtime, just FYI.

这个代码当然可以修改为更复杂,我只是做了一个相对简单的例子来说明它是如何完成的:

This code can of course be modified to be more complex, I just did a relatively simple example to show how it can be done:

public class AddRangeOfValuesBehavior : Behavior<ItemsControl>
	{
		public static readonly DependencyProperty MinValueProperty = DependencyProperty.Register("MinValue", typeof(double), typeof(AddRangeOfValuesBehavior), new PropertyMetadata(0.0, MinValueChanged));
		public static readonly DependencyProperty MaxValueProperty = DependencyProperty.Register("MaxValue", typeof(double), typeof(AddRangeOfValuesBehavior), new PropertyMetadata(100.0, MaxValueChanged));
		public static readonly DependencyProperty DeltaProperty = DependencyProperty.Register("Delta", typeof(double), typeof(AddRangeOfValuesBehavior), new PropertyMetadata(1.0, DeltaChanged));
		public static readonly DependencyProperty DirectionProperty = DependencyProperty.Register("Direction", typeof(bool), typeof(AddRangeOfValuesBehavior), new PropertyMetadata(true, DirectionChanged));

		public double MinValue
		{
			get { return (double)this.GetValue(MinValueProperty); }
			set { this.SetValue(MinValueProperty, value); }
		}

		public double MaxValue
		{
			get { return (double)this.GetValue(MaxValueProperty); }
			set { this.SetValue(MaxValueProperty, value); }
		}

		public double Delta
		{
			get { return (double)this.GetValue(DeltaProperty); }
			set { this.SetValue(DeltaProperty, value); }
		}

		public bool Direction
		{
			get { return (bool)this.GetValue(DirectionProperty); }
			set { this.SetValue(DirectionProperty, value); }
		}


		public AddRangeOfValuesBehavior()
		{
		}

		protected override void OnAttached()
		{
			base.OnAttached();
			this.UpdateItemSource();
		}

		protected override void OnDetaching()
		{
			base.OnDetaching();
		}

		private void UpdateItemSource()
		{
			if(this.AssociatedObject != null)
			{
				this.AssociatedObject.ItemsSource = null;
				this.AssociatedObject.ItemsSource = this.CreateValueSource();
			}
		}

		private IEnumerable<double> CreateValueSource()
		{
			List<double> values = new List<double>();
			if (this.Direction)
			{
				// count up
				double currentValue = this.MinValue;
				while(currentValue <= this.MaxValue)
				{
					values.Add(currentValue);
					currentValue += this.Delta;
				}
			}
			else
			{
				// count down
				double currentValue = this.MaxValue;
				while (currentValue >= this.MinValue)
				{
					values.Add(currentValue);
					currentValue -= this.Delta;
				}

			}
			return values;
		}

		private static void MinValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			AddRangeOfValuesBehavior behavior = d as AddRangeOfValuesBehavior;
			if (behavior != null)
			{
				behavior.UpdateItemSource();
			}
		}

		private static void MaxValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			AddRangeOfValuesBehavior behavior = d as AddRangeOfValuesBehavior;
			if (behavior != null)
			{
				behavior.UpdateItemSource();
			}
		}

		private static void DeltaChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			AddRangeOfValuesBehavior behavior = d as AddRangeOfValuesBehavior;
			if (behavior != null)
			{
				behavior.UpdateItemSource();
			}
		}

		private static void DirectionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			AddRangeOfValuesBehavior behavior = d as AddRangeOfValuesBehavior;
			if (behavior != null)
			{
				behavior.UpdateItemSource();
			}
		}

	}


这篇关于SketchFlow下拉值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆