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

21,082

Solution 1

I'm not sure what you mean exactly by saying:an element Name[2] of type String, so here are two possible solutions to your problem: Array1 and String1. Array1 show bow to bind to element of an array and String1 shows how to display one single character in a string.

CODE:

 public partial class MainWindow : Window
{
    private Array array1 = new[] {"test1", "test2", "test3"};
    public Array Array1 { get { return array1; } }

    public string string1 = "string";
    public string String1 { get { return string1; } }

    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
    }
}

XAML:

 <StackPanel Orientation="Vertical">
    <TextBlock Text="{Binding Array1[0]}"/>
    <TextBlock Text="{Binding Array1[2]}"/>
    <TextBlock Text="{Binding String1[0]}"/>
    <TextBlock Text="{Binding String1[1]}"/>
</StackPanel>

Hope that helps.

Solution 2

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:

<TextBox Margin="5" Text="{Binding SomeNorthEasternStates[3], Mode=Default}"/>

Solution 3

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"));
    }
}

Solution 4

if you mean that if we can bind array item with textBox then, yes

 <TextBox Margin="10" Text="{Binding Name[2], Mode=Default}"  Name="textBox1"/>

Solution 5

Use ObservableCollection instead:

private ObservableCollection<string> _myItems = new ObservableCollection<string>(new[] { "test1", "test2", "test3" });

public ObservableCollection<string> MyItems
{
    get { return _myItems; }
    set { _myItems = value; }
}

Xaml

    <StackPanel Orientation="Vertical">
        <TextBox Text="{Binding MyItems[0]}"/>
        <TextBox Text="{Binding MyItems[2]}"/>
        <TextBlock Text="{Binding MyItems[0]}"/>
        <TextBlock Text="{Binding MyItems[1]}"/>
    </StackPanel>
Share:
21,082
Scifiballer24
Author by

Scifiballer24

Updated on July 05, 2022

Comments

  • Scifiballer24
    Scifiballer24 almost 2 years

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

  • Jonathan Allen
    Jonathan Allen over 13 years
    TextBox doesn't have a Content property.
  • Can Sahin
    Can Sahin over 13 years
    Is there a particular reason why you are using the methods of the Array class instead of doing simply var testArray = new string[5]; testarray[0] = "NY"; ....
  • John Bowen
    John Bowen over 13 years
    @Heinzi - Or var testArray = new [] { "NH", "VT", "CT", "MA", "ME" }; (NY and NJ aren't in New England) :)
  • SKG
    SKG over 13 years
    @John Bowen. My bad. Changed the name.
  • SKG
    SKG over 13 years
    @Heinzi. No specific reason. See Edit. 7 hrs later with some sleep and coffee I am wondering why I did it that way. :)
  • Scifiballer24
    Scifiballer24 over 13 years
    Thanks, this works except that updating the source doesn't update the target control. I've tried everything and it isn't working so far. Should I be using an ObservableCollection?
  • klm_
    klm_ over 13 years
    Since this comment doesn't allow I've posted another answer.
  • Scifiballer24
    Scifiballer24 over 13 years
    Thanks. ObservableCollection implements those features as well and is working perfectly. I appreciate your help klm_.