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

后端 未结 5 1919
予麋鹿
予麋鹿 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:11

    Yes you can. Following is the XAML approach. It is advisable to bind to an Observable collection if you want to update the UI automatically when the value changes.

    public class DataStub
    {
        public Array SomeNorthEasternStates
        {
            get
            {
                return new[] { "NH", "VT", "CT", "MA", "ME" };      
            }
        }
    }
    

    XAML: Assuming the DataContext is set correctly:

    
    

提交回复
热议问题