2013-10-08 29 views
0

我在C#中的類,它看起來就像這樣:數據網格與自定義的類C#WPF

class Program 
{ 
    string name; 
    float number; 
    string type; 
    Value[] parameters=new Value[40]; 
} 
class Value 
{ 
    float parameter; 
    string parameter name; 
} 

另外,我有一個DataGrid是應該是這樣的:

第3列是Number,Name,Type。 其他是40個參數的名稱Value[index].parameterName).

在應該代表一個Program對象,並顯示3個屬性值numbernametypeDataGrid每一行,接着Value[index].parameter的值。

Values大小是動態分配的,我寧願主要使用c#代碼而不使用xaml。 此外,我需要更改單元格中的值(數字除外)。

有沒有我能夠實現的事件?

謝謝!

+0

是否要在分隔的列中顯示每個參數? –

+0

我認爲'Value [index] .parameter'你的意思是'parameters [index] .parameter'? –

+0

我想要在不同的列中的每個參數。 是的,對不起,我的意思是參數[index] .parameter –

回答

0

所有我想建議你應有的價值類而不是數組列表(當你的大小不是預先知道的),如

class Program 
{ 
    string name; 
    float number; 
    string type; 
    List<Value> parameters; 
} 

現在你應該你的DataGrid,以顯示內部創建DataGridTemplateColumn第一您的動態尺寸參數如

<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Program}"> 
     <DataGrid.Columns> 
      <DataGridTemplateColumn> 
       <DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
         <ListBox ItemsSource="{Binding parameters}"> 
          <ListBox.ItemsPanel> 
           <ItemsPanelTemplate> 
            <StackPanel IsItemsHost="True" Orientation="Horizontal"/> 
           </ItemsPanelTemplate> 
          </ListBox.ItemsPanel> 
         </ListBox> 
        </DataTemplate> 
       </DataGridTemplateColumn.CellTemplate> 
      </DataGridTemplateColumn> 
     </DataGrid.Columns> 
    </DataGrid> 
+0

謝謝,但是可以用C#代碼來完成嗎? –

+0

你在c#代碼隱藏中實現了什麼嗎? –

+1

@ItayAzolayAmsalem來自MSDN:_以編程方式創建模板的推薦方式是使用XamlReader類的Load方法從字符串或內存流加載XAML._ –