2012-10-02 1464 views
1

可以说我有3个或更多滑块和每个滑块可以具有从值0到100 但是我想,所有滑块值的总和是< = 100。在情况下,我有4对滑块每个人的最大值将是25如何阻止wpf UI以避免非法状态?

每个滑块都绑定到双变量,每次用户使用滑块(节拍频率0.1)时,我计算总和并设置其他滑块,或者根据需要设置相同的滑块,以使总和为< = 100。

的问题是,该计算需要的时间体面量并在此期间用户可以设置非法值。我想通过阻止UI直到计算结束来解决这个问题。基本上与理想的响应性相反。

其他意见和建议,解决了滑盖的事情是值得欢迎的。

For example 3 slider.

滑块结合

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); 
       } 
      } 
     } 
    } 
} 
+1

你是一个整数绑定到一个滑盖的0.1蜱? – databyss

+0

我的错误当然是双重的。 – steffan

+0

假设滑块处于面板,设置在面板为禁用在你的计算开始和结束时重新启用。 – databyss

回答

1

它看起来像你没有利用的数据绑定下面是一个简单的例子 - 只需将计算逻辑添加到计算方法中,界面会自动更新,注意这是一个简单的例子,我不确定我是否会这样实现它。在你的数字中使用小数。如果您将外部语言/区域设置与逗号作为小数点分隔符一起使用,则会出错。

<Window x:Class="WpfApplication3.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <StackPanel> 
      <Slider Margin="10" Value="{Binding Path=Value1}" /> 
      <TextBlock Text="{Binding Path=Value1}" /> 
      <Slider Margin="10" Value="{Binding Path=Value2}" /> 
      <TextBlock Text="{Binding Path=Value2}" /> 
      <Slider Margin="10" Value="{Binding Path=Value3}" /> 
      <TextBlock Text="{Binding Path=Value3}" /> 
     </StackPanel> 

    </Grid> 
</Window> 

代码隐藏(MVVM的做法,这将是您的视图模型)

namespace WpfApplication3 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window, INotifyPropertyChanged 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
      DataContext = this; 
     } 

     private double _value1; 
     public double Value1 
     { 
      get { return _value1; } 
      set 
      { 
       if(value != _value1) 
       { 
        _value1 = value; 
        DoMyCalculations(_value1, _value2, _value3); 
        NotifyPropertChanged("Value1"); 
       } 
      } 
     } 
     private double _value2; 
     public double Value2 
     { 
      get { return _value2; } 
      set 
      { 
       if (value != _value2) 
       { 
        _value2 = value; 
        DoMyCalculations(_value1, _value2, _value3); 
        NotifyPropertChanged("Value2"); 
       } 
      } 
     } 
     private double _value3; 
     public double Value3 
     { 
      get { return _value3; } 
      set 
      { 
       if (value != _value3) 
       { 
        _value3 = value; 
        DoMyCalculations(_value1, _value2, _value3); 
        NotifyPropertChanged("Value3"); 
       } 
      } 
     } 
     private bool isCalculating = false; 
     private void DoMyCalculations(double value1, double value2, double value3) 
     { 
      if (isCalculating) 
       return; 
      isCalculating = true; 

      // Perform logic to reset here 


      isCalculating = false; 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 

     /// <summary> 
     /// Notify of Property Changed event 
     /// </summary> 
     /// <param name="propertyName"></param> 
     public void NotifyPropertChanged(string propertyName) 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 
    } 
} 
+0

我大致使用了这种方法。第一:锁定方法。第二:拒绝所有传入的更改,直到计算完成。 – steffan