我需要创建一个应用程序,用户可以在其中输入不同的多项式函数。首先,用户必须选择多项式函数的某个等级(0到10之间)。根据所选的等级,应该出现不同数量的文本框,用户可以在其中指定系数的值。例如,用户选择等级“ 4”-> 5出现文本框。它应该看起来像这样:
a4 ___ a3___ 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();
});
}
}
答案 0 :(得分:1)
根据评论,我将提供一个小示例,说明如何完成此操作(我添加了1-2个附加功能,可能对此很方便)
使用Text="{Binding Value}"
绑定到VM中的值
使用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); }
}
}