Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 666
Scifiballer24 Avatar asked Oct 23 '10 04:10

Scifiballer24


1 Answers

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.

like image 51
klm_ Avatar answered Sep 27 '22 16:09

klm_