我需要创建一个应用程序,用户可以在其中输入不同的多项式函数。首先,用户必须选择特定等级的多项式函数(在0和10之间)。在所选等级的基础上,应该出现不同数量的文本框,用户可以在其中指定系数的值。例如,用户选择等级“4” - >出现5个文本框。它应该如下所示:
___我___ a2 ___ a1 ___ a0 ___
___:代表单个文本框
在使用ItemsControl时,我也在努力水平对齐文本框。我还想将用户输入的值保存在ViewModel中。我已经尝试了很多东西,但我无法弄明白该怎么做。到目前为止这是我的代码:
<ItemsControl ItemsSource="{Binding SelectedGrade}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox Height="20" Width="100"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
public ObservableCollection<double> SelectedGrade
{
get
{
ObservableCollection<double> newCol = new ObservableCollection<double>();
for (int i = 0; i < this.SelectedNum + 1; i++)
{
newCol.Add(0);
}
this.selectedGrade = newCol;
return newCol;
}
set
{
//...
}
}
public ICommand AddPolyFuncCommand
{
get
{
return new Command(obj =>
{
Function newPolyFunc = new PolyFunction(this.Coefficients);
Functions.Add(newPolyFunc);
CalculatePoints();
});
}
}
根据评论,我将提供一个小例子如何做(我添加了1-2个额外的,这可能是方便的)
使用Text="{Binding Value}"
绑定到VM中的Value
使用Wrappanel
水平显示它
(可选)使用AlternationIndex
标记系数
(可选)更改FlowDirection
以显示它,就像您勾画它一样
<!-- Combobox to select from the available Grades and store the selected in the SelectedGrade -->
<ComboBox ItemsSource="{Binding AvailableGrades}" SelectedValue="{Binding SelectedGrade}" VerticalAlignment="Top" HorizontalAlignment="Left"/>
<!-- Use Alternationcount to label the coefficients properly and Flowdirection to keep a0 on the right side -->
<ItemsControl ItemsSource="{Binding Coefficients}" AlternationCount="11" FlowDirection="RightToLeft">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<!-- Textbox to enter Coefficient (eg. a0 would be SelectedGrade[0] in code)-->
<TextBox Text="{Binding Value}" Width="50" VerticalAlignment="Center"/>
<!-- Labeling of the Coefficient using the AlternationIndex and a String Format -->
<Label Content="{Binding Path=(ItemsControl.AlternationIndex), RelativeSource={RelativeSource TemplatedParent}}" ContentStringFormat="a{0}"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
<!-- Use a WrapPanel as ItemsPanel to align the Entries horizontally -->
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
编辑
要正确调整系数的数量,需要一点点逻辑,但首先 - >适当地重命名属性并定义它们的可能值(这有助于为属性创建适当的逻辑)
AvailableGrades = 0,1,2 ... 10
SelectedGrade∈{0,1,2 ... 10}
系数= a(0),a(1)... a(SelectedGrade)
//Unfortunately it is not possible to use a Value Type and bind to it due it has no Getter/Setter therefore we need a little wrapper
public class ValueTypeAsClass<T>
{
public T Value { get; set; }
public static implicit operator ValueTypeAsClass<T>(T v)
{
return new ValueTypeAsClass<T> { Value = v };
}
public static implicit operator T(ValueTypeAsClass<T> v)
{
return v.Value;
}
}
//member variable for select grade
private int _selectedGrade = 0;
//List of Coefficients (renamed from SelectedGrade)
public ObservableCollection<ValueTypeAsClass<double>> Coefficients { get; set; } = new ObservableCollection<ValueTypeAsClass<double>>() { 0d };
//Available (valid) Grades to select from in the ComboBox
public List<int> AvailableGrades { get; private set; } = Enumerable.Range(0, 11).ToList();
//Currently selected grad with logic to adjust the coefficient amount
public int SelectedGrade
{
get { return _selectedGrade; }
set
{
_selectedGrade = value;
//Clear Coefficients and add the necessary amount
Coefficients.Clear();
for (int i = 0; i <= _selectedGrade; i++) { Coefficients.Add(0); }
}
}