且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

绑定到一个数组元素

更新时间:2022-12-10 22:31:43

无需转换。您可以直接绑定到编曲[0] 像这样

No need for the converter. You can bind directly to Arr[0] like this

 <TextBlock Name="testBox" Text="{Binding Path=Arr[0]}"/>

编曲元素需要实现 INotifyPropertyChanged的虽然以动态更新。

The elements in Arr would need to implement INotifyPropertyChanged though in order to dynamically update.

更新:为了详细一​​点:

Update: To elaborate a bit more:

public class MyDouble : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private double _Value;
    public double Value 
    { 
        get { return _Value; } 
        set { _Value = value; OnPropertyChanged("Value"); }
    }

    void OnPropertyChanged(string propertyName)
    {
       var handler = PropertyChanged;
       if (handler != null)
       {
          handler(this, new PropertyChangedEventArgs(propertyName));
       }
    }
}

然后

 ObservableCollection<MyDouble> Arr { get; set; }

和绑定到

 <TextBlock Name="testBox" Text="{Binding Path=Arr[0].Value}"/>