Syaiful Nizam Yahya
Syaiful Nizam Yahya

Reputation: 4305

Array of class from code behind to xaml

I want to display in xaml data of class array defined in app.xaml.cs.

Heres what i did.

Class that holds the data. Person.cs

public class Person : INotifyPropertyChanged
{
    #region Constructors and Destructors

    public Person(int id, string name)
    {
        this.Name = name;
        this.Id = id;
    }

    #endregion

    #region Public Events

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion

    #region Public Properties

    public int Id { get; set; }

    public string Name { get; set; }

    #endregion

    #region Methods

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    #endregion
}

Person is declared in App.xaml.cs like so.

public partial class App : Application
{
    public static Person[] People =
        {
            new Person(1, "John"),
            new Person(2, "Jake"),
            new Person(3, "James"),
            new Person(4, "Jack")
        };
}

I want to display the data in xaml. I cant seem to figure out how to do it.

<StackPanel>
    <StackPanel Orientation="Horizontal">
        <TextBox Height="23" TextWrapping="Wrap" Text="id"/>
        <TextBox Height="23" TextWrapping="Wrap" Text="name"/>
    </StackPanel>
    <StackPanel Orientation="Horizontal">
        <TextBox Height="23" TextWrapping="Wrap" Text="id"/>
        <TextBox Height="23" TextWrapping="Wrap" Text="name"/>
    </StackPanel>
    <StackPanel Orientation="Horizontal">
        <TextBox Height="23" TextWrapping="Wrap" Text="id"/>
        <TextBox Height="23" TextWrapping="Wrap" Text="name"/>
    </StackPanel>
    <StackPanel Orientation="Horizontal">
        <TextBox Height="23" TextWrapping="Wrap" Text="id"/>
        <TextBox Height="23" TextWrapping="Wrap" Text="name"/>
    </StackPanel>
</StackPanel>

Thanks.

Upvotes: 0

Views: 789

Answers (2)

Tor Langlo
Tor Langlo

Reputation: 191

Normally using an ItemsControl or one of its subclasses is the way to go. If you wanted the xaml exactly as entered in the original post, this would be the way to do it:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:a="clr-namespace:WpfApplication1"
    Title="MainWindow" Height="350" Width="525">
  <StackPanel DataContext="{x:Static a:App.People}">
    <StackPanel Orientation="Horizontal">
      <TextBox Height="23" TextWrapping="Wrap" Text="{Binding [0].Id}"/>
      <TextBox Height="23" TextWrapping="Wrap" Text="{Binding [0].Name}"/>
    </StackPanel>
    <StackPanel Orientation="Horizontal">
      <TextBox Height="23" TextWrapping="Wrap" Text="{Binding [1].Id}"/>
      <TextBox Height="23" TextWrapping="Wrap" Text="{Binding [1].Name}"/>
    </StackPanel>
    <StackPanel Orientation="Horizontal">
      <TextBox Height="23" TextWrapping="Wrap" Text="{Binding [2].Id}"/>
      <TextBox Height="23" TextWrapping="Wrap" Text="{Binding [2].Name}"/>
    </StackPanel>
    <StackPanel Orientation="Horizontal">
      <TextBox Height="23" TextWrapping="Wrap" Text="{Binding [3].Id}"/>
      <TextBox Height="23" TextWrapping="Wrap" Text="{Binding [3].Name}"/>
    </StackPanel>
  </StackPanel>
</Window>

For more about property paths in xaml, take a look here: http://msdn.microsoft.com/en-us/library/ms742451(v=vs.110).aspx.

Upvotes: 1

devdigital
devdigital

Reputation: 34349

You should use an collection control, for example an ItemsControl and bind its ItemsSource to your static property:

<ItemsControl ItemsSource="{Binding ...}">
   <ItemTemplate>
      <DataTemplate>
         <StackPanel Orientation="Horizontal">
            <TextBox Height="23" TextWrapping="Wrap" Text="{Binding Id}" />
            <TextBox Height="23" TextWrapping="Wrap" Text="{Binding Name}" />
         </StackPanel>
      </DataTemplate>
   </ItemTemplate>
</ItemsControl>

You should not use a static property, but instead have a service which is injected into your view model (which is acting as the data context for this view), and ensure this service is a singleton.

You will also need to invoke the PropertyChanged event in the setters of your Person properties for the UI to be notified.

If you want to persist with the static property, then look here.

Upvotes: 3

Related Questions