提问者:小点点

将文本框绑定到列表<>[i]


我的对象看起来如下所示:

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...) ,但我在想一个方法,使我的目标更容易。


共1个答案

匿名用户

您可以创建绑定到ListParamsItemsControl,并将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}" />