0

app.xaml.csで定義したクラス配列のデータをxamlで表示したい。

これが私がしたことです。

データを保持するクラス。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 は App.xaml.cs で次のように宣言されます。

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

xamlでデータを表示したい。私はそれを行う方法を理解できないようです。

<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>

ありがとう。

4

2 に答える 2

1

通常は、ItemsControlまたはそのサブクラスの 1 つを使用する方法です。元の投稿で入力したとおりの xaml が必要な場合は、次のようにします。

<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>

xaml のプロパティ パスの詳細については、http://msdn.microsoft.com/en-us/library/ms742451(v=vs.110).aspx を参照してください。

于 2013-04-09T18:07:46.990 に答える