【发布时间】:2012-10-02 01:22:29
【问题描述】:
假设我有 3 个或更多滑块,每个滑块的值可以从 0 到 100。 但是我希望所有滑块值的总和为
每个滑块都绑定到一个双变量,每次用户使用滑块(滴答频率 0.1)时,我都会计算总和并将其他滑块向后设置,或者如果需要,将相同的滑块向后设置,以便总和为
问题是,计算需要相当长的时间,同时用户可以设置非法值。我想通过阻止 UI 直到计算结束来解决这个问题。基本上与所需的响应能力相反。
欢迎其他解决滑块问题的想法和建议。
滑块绑定
public BindingList<WLCToolParameter> WLCParameter
{
get { return _toolParameter; }
set { _toolParameter = value; }
}
应该是即时的 - 不是真的:(
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MCDA.Entity;
using MCDA.Extensions;
namespace MCDA.Model
{
class ProportionalDistributionStrategy : IWeightDistributionStrategy
{
public void Distribute<T>(IList<T> listOfToolParameter) where T : class, IToolParameter
{
if (listOfToolParameter.Count == 0)
return;
IToolParameter lastWeightChangedToolParameter = lastWeightChangedToolParameter = listOfToolParameter[0].LastWeightChangedToolParameter;
double sumOfAllWeights = listOfToolParameter.Sum(t =>t.Weight);
//we have to rescale
if (sumOfAllWeights > 100)
{
double overrun = sumOfAllWeights - 100;
//how much do we have without the locked and the last changed?
double availableSpace = listOfToolParameter.Where(t => t.IsLocked == false && t != lastWeightChangedToolParameter).Sum(t => t.Weight);
//we have enough by taking from the non locked
if (availableSpace > overrun)
{
//lets remove proportional
double sumOfChangeableWeights = listOfToolParameter.Where(t => t.IsLocked == false && t != lastWeightChangedToolParameter).Sum(t => t.Weight);
//in case we have only one element that is suitable we can directly remove all from this one
if (listOfToolParameter.Where(t => t.IsLocked == false && t.Weight > 0 && t != lastWeightChangedToolParameter).Count() == 1)
{
listOfToolParameter.Where(t => t.IsLocked == false && t.Weight > 0 && t != lastWeightChangedToolParameter).ForEach(t => t.Weight = t.Weight - overrun);
return;
}
listOfToolParameter.Where(t => t.IsLocked == false && t.Weight > 0 && t != lastWeightChangedToolParameter).ForEach(t => t.Weight = t.Weight - (sumOfChangeableWeights / (sumOfChangeableWeights - t.Weight)) * overrun);
}
//we have to resize also the latest change, but we try to keep as much as possible of the latest change
else
{
//lets set them to zero
listOfToolParameter.Where(t => t.IsLocked == false && t != lastWeightChangedToolParameter).ForEach(t => t.Weight = 0);
//how much are we still over?
double stillOver = listOfToolParameter.Sum(t => t.Weight) - 100;
//and cut from the last changed
listOfToolParameter.Where(t => t == lastWeightChangedToolParameter).ForEach(t => t.Weight -= stillOver);
}
}
}
}
}
【问题讨论】:
-
您将一个整数绑定到一个刻度为 0.1 的滑块?
-
我的错误当然是双重的。
-
假设滑块在面板中,在计算开始时将面板设置为禁用并在结束时重新启用。
-
其实我不喜欢这个想法,因为那是我第一次不得不修改后面的代码,而不是只使用视图模型。
-
你能发布一些代码吗?这些计算应该是即时的。我看不出您有任何需要屏蔽的理由。
标签: c# wpf multithreading user-interface slider