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

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

+7
c # data-binding wpf mvvm
source share
5 answers

I'm not sure what you mean by saying: the Name [2] element is of type String, so here are two possible solutions to your problem: Array1 and String1. Array1 shows a bow for binding to an array element, and String1 shows how to display a 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 this helps.

+12
source share

Yes, you can. The following is the XAML approach. It is advisable to bind to the Observable collection if you want to automatically update the user interface when the value changes.

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

XAML: Suppose the DataContext is set correctly:

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

I added a button in xaml and signed the "click" event.

Here is the 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")); } } 
+3
source share

if you mean that if we can associate an array element with a textBox, then yes

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

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> 
+1
source share

All Articles