C# 如何阻止wpf UI以避免非法状态?
假设我有3个或更多滑块,每个滑块可以有0到100之间的值。 但是,我希望所有滑块值的总和为100) { 双超限=总重量-100; //如果没有锁好的和最后一次换的,我们有多少钱? double availableSpace=listOfToolParameter.Where(t=>t.IsLocked==false&&t!=lastWeightChangedToolParameter.Sum(t=>t.Weight); //我们有足够的时间从非锁定的 如果(可用空间>超限) { //让我们按比例删除 double sumOfChangeableWeights=listOfToolParameter.Where(t=>t.IsLocked==false&&t!=lastWeightChangedToolParameter.Sum(t=>t.Weight); //如果我们只有一个合适的元素,我们可以直接从这个元素中删除所有元素 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-over); 返回; } listOfToolParameter.Where(t=>t.IsLocked==false&&t.Weight>0&&t!=lastWeightChangedToolParameter.ForEach(t=>t.Weight=t.Weight-(变化权重之和/(变化权重之和-t.Weight))*超限); } //我们还必须根据最新的变化调整大小,但我们尽量保持最新的变化 其他的 { //让我们把它们设为零 其中(t=>t.IsLocked==false&&t!=lastWeightChangedToolParameter).ForEach(t=>t.Weight=0); //我们还有多少钱? double stillOver=listOfToolParameter.Sum(t=>t.Weight)-100; //从上次换的衣服上剪下来 其中(t=>t==lastWeightChangedToolParameter).ForEach(t=>t.Weight-=stillOver); } } } } }C# 如何阻止wpf UI以避免非法状态?,c#,wpf,multithreading,user-interface,slider,C#,Wpf,Multithreading,User Interface,Slider,假设我有3个或更多滑块,每个滑块可以有0到100之间的值。 但是,我希望所有滑块值的总和为100) { 双超限=总重量-100; //如果没有锁好的和最后一次换的,我们有多少钱? double availableSpace=listOfToolParameter.Where(t=>t.IsLocked==false&&t!=lastWeightChangedToolParameter.Sum(t=>t.Weight); //我们有足够的时间从非锁定的 如果(可用空间>超限) { //让我们按比例
看起来您没有使用数据绑定。下面是一个简单的例子-只需将您的计算逻辑添加到计算方法中。用户界面将自我更新。请注意,这是一个粗略的示例。我不确定我是否会以这种方式实施它。还要注意在数字中使用小数。如果将其用于以逗号作为小数分隔符的外语/区域设置,则会出错
<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方法这将在您的视图模型中)
命名空间WpfApplication3
{
///
///MainWindow.xaml的交互逻辑
///
公共部分类主窗口:窗口,INotifyPropertyChanged
{
公共主窗口()
{
初始化组件();
DataContext=this;
}
私人双重价值1;
公共双重价值1
{
获取{return\u value1;}
设置
{
如果(值!=\u值1)
{
_value1=值;
域计算(_值1,_值2,_值3);
NotifyPropertChanged(“Value1”);
}
}
}
私人双重价值2;
公众双重价值2
{
获取{return\u value2;}
设置
{
如果(值!=\u值2)
{
_value2=值;
域计算(_值1,_值2,_值3);
NotifyPropertChanged(“Value2”);
}
}
}
私人双重价值3;
公众双重价值3
{
获取{return\u value3;}
设置
{
如果(值!=\u值3)
{
_value3=值;
域计算(_值1,_值2,_值3);
NotifyPropertChanged(“Value3”);
}
}
}
私有布尔运算=假;
私有无效域计算(双值1、双值2、双值3)
{
如果(正在计算)
返回;
正在计算=正确;
//在此处执行逻辑重置
正在计算=错误;
}
公共事件属性更改事件处理程序属性更改;
///
///通知属性更改事件
///
///
public void NotifyPropertChanged(字符串propertyName)
{
if(PropertyChanged!=null)
{
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
}
}
}
看起来您没有使用数据绑定。下面是一个简单的例子-只需将您的计算逻辑添加到计算方法中。用户界面将自我更新。请注意,这是一个粗略的示例。我不确定我是否会以这种方式实施它。还要注意在数字中使用小数。如果将其用于以逗号作为小数分隔符的外语/区域设置,则会出错
<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方法这将在您的视图模型中)
命名空间WpfApplication3
{
///
///MainWindow.xaml的交互逻辑
///
公共部分类主窗口:窗口,INotifyPropertyChanged
{
公共主窗口()
{
初始化组件();
DataContext=this;
}
脉波重复间隔
<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>
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));
}
}
}
}