0

我找不到可以解决此问题的答案。我有一个 ObservableCollection

这是我的班级:

public class myClass
    {
      public string name;
      public int[] dimensions = new int[2];
    }

这是设置 ObservableCollection 的代码:

public class roomBuilder
{
  private ObservableCollection<myClass> rooms;

      public roomBuilder() //constructor
      {
        string roomName;
        int[] dimensions = new int[2];
        myClass newRoom = new myClass();
        rooms = new ObservableCollection<room>();
        roomName = "Hall";
        dimensions[0] = 10;
        dimensions[1] = 12;
        newRoom.name = roomName;
        newRoom.dimensions = dimensions;
        rooms.Add(newRoom);
        roomListBox.DataContext = rooms;
      }

用于此的 XAML 是:

    <ListBox Canvas.Left="-1" Canvas.Top="47" Height="419" Name="roomListBox" Width="481"  BorderThickness="1" BorderBrush="GhostWhite" ItemsSource="{Binding}" DataContext="{Binding}"> 

        <ListBox.ItemTemplate  >
            <DataTemplate   >
                <StackPanel Orientation="Vertical" Margin="1" Name="verstack"   > 
                    <StackPanel Orientation="Horizontal" Margin="1" KeyDown="StackPanel_KeyDown" >
                        <TextBox Text="{Binding  Path=name}" IsReadOnly="False" FontFamily="Courier New" FontSize="22" Height="65" VerticalAlignment="Top" TextAlignment="Center"/>
                        <TextBox Name="xDimension" Text="{Binding Mode=TwoWay, Path=dimensions[0] }"  Width="70" Height="65" VerticalAlignment="Top" IsReadOnly="False" FontFamily="Courier New" Margin="2,0,1,0" FontSize="22" MaxLength="3" InputScope="TelephoneNumber" TextAlignment="Center"  />
                    <TextBlock Name="separator" Text=":" FontSize="32"  FontWeight="ExtraBold" Margin="1,4,1,0"  />
                        <TextBox Name="yDimension" Text="{Binding Mode=TwoWay, Path=dimensions[1] }" Width="70" Height="65" VerticalAlignment="Top"   IsReadOnly="False" FontFamily="Courier New" Margin="1,0,2,0" FontSize="22" MaxLength="3" InputScope="TelephoneNumber" TextAlignment="Center"/>

我还没有在这里关闭 Xaml,它下面还有更多负载,当我运行它时没有显示任何内容,我只是不知道问题是什么,我已经敲了 2 天,请有人帮忙

4

1 回答 1

0

房间需要是您绑定到的公共属性。

public ObservableCollection<myClass> rooms
{ 
   get { return ........
于 2012-06-03T23:50:17.750 回答