In C# and WPF, can you bind an element of an array to an objects property?

后端 未结 5 1909
予麋鹿
予麋鹿 2021-02-15 14:55

For example, is it possible to bind a Textblock\'s Text property to an element Name[2] of type String?

5条回答
  •  迷失自我
    2021-02-15 15:23

    I've added a button in xaml and subscribed "click" event.

    Here is C# code.

    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        private Array array1 = new[] { "test1", "test2", "test3" };
        public Array Array1 { get { return array1; } }
    
    
        public string string1 = "string";
        public string String1
        {
            get { return string1; }
            set
            {
                string1 = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("String1"));
            }
        }
    
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;
    
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            String1 = DateTime.Now.ToString();
            array1.SetValue("another test", 0); 
            PropertyChanged(this, new PropertyChangedEventArgs("Array1"));
        }
    }
    

提交回复
热议问题