我的对象看起来如下所示:
public class Macro : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
private string name = "";
public string Name
{
get { return this.name; }
set
{
this.name = value;
this.NotifyPropertyChanged("Name");
}
}
private ObservableCollection<int> listParams = new ObservableCollection<int>();
public ObservableCollection<int> ListParams
{
get { return this.listParams; }
set
{
this.listParams = value;
this.NotifyPropertyChanged("ListParams");
}
}
public Macro()
{
}
public Macro(string nom)
{
this.Name = nom;
}
}
在XAML中,我想创建一个绑定到ListParams[0],ListParams[1].。。 ListParams[20]。 这有没有可能做这样一个捆绑呢? 到目前为止,我jsut创建了20个“参数”(int p0,int p1,int p2...) ,但我在想一个方法,使我的目标更容易。
您可以创建绑定到ListParams
的ItemsControl
,并将TextBox
放入ItemTemplate
:
<ItemsControl ItemsSource="{Binding ListParams}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=., Mode=OneWay}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
由于int
是不可变的,因此您无法使用textbox
更改它。 如果需要,应将源集合的类型从ObservableCollection
更改为ObservableCollection
,其中yourClass
是一个具有int
属性的类,然后绑定到该属性:
<TextBox Text="{Binding Path=IntPropertyOfYourClass}" />