2012-10-02 66 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