2016-02-05 77 views
1

我有一個這樣的對象:WPF數據網格:如何顯示對象,具有雙值

public class MChiStructure 
{ 
    [System.Runtime.Serialization.OptionalField(VersionAdded = 2)] 
    public double chiV1Plus; 
    [System.Runtime.Serialization.OptionalField(VersionAdded = 2)] 
    public double chiV1Minus; 
    [System.Runtime.Serialization.OptionalField(VersionAdded = 2)] 
    public double mV1Plus; 
... 
} 

和我想有一個數據網格表示,在第一列中,變量名和,在第二列,雙重值。 我也想知道是否有隱藏某些值的可能性(例如,如果是負的,必須被隱藏)

感謝所有

+0

你確定你想要DataGrid嗎?在我看來,最好是使用ListView?爲什麼選擇DataGrid? – StepUp

+0

你如何使用ListView?用您的解決方案發布答案​​,謝謝! – FrancescoDS

回答

1

它,它的背後是應用程序代碼中,在第一你應該創建一個模型類並填充它的代碼隱藏。就這樣。請,看一個例子:

型號:窗口的

public class MChiStructure 
{ 
    public string TitleField { get; set; } 

    public double chiV1Plus { get; set; } 

    public double chiV1Minus { get; set; }   

    public double mV1Plus { get; set; } 
} 

代碼隱藏:

public MainWindow() 
{ 
    InitializeComponent(); 
    FillDataGrid(); 
} 

private void FillDataGrid() 
{ 
    ObservableCollection<MChiStructure> coll = new ObservableCollection<MChiStructure>(); 
    for (int start = 0; start < 10; start++) 
    { 
     coll.Add(new MChiStructure(){TitleField="Title " + start.ToString(), 
     chiV1Minus=start-1, chiV1Plus=start+1, mV1Plus=start-1});         
    } 
    dataGrid.ItemsSource = coll;  
} 

和XAML:

<DataGrid Name="dataGrid"/> 

更新。使用ListView控件:

XAML:

<ListView Name="listView"> 
    <ListView.ItemTemplate> 
     <DataTemplate> 
     <Border BorderBrush="Brown" BorderThickness="1" CornerRadius="5"> 
      <StackPanel> 
       <StackPanel Orientation="Horizontal"> 
        <TextBlock Text="chiV1Plus:" Margin="2" FontWeight="Bold"/> 
        <TextBlock Text="{Binding chiV1Plus}" Margin="2" /> 
       </StackPanel> 
       <StackPanel Orientation="Horizontal"> 
        <TextBlock Text="chiV1Minus:" Margin="2" FontWeight="Bold"/> 
        <TextBlock Text="{Binding chiV1Minus}" Margin="2" /> 
       </StackPanel> 
       <StackPanel Orientation="Horizontal"> 
        <TextBlock Text="mV1Plus:" Margin="2" FontWeight="Bold" /> 
        <TextBlock Text="{Binding mV1Plus}" Margin="2" /> 
       </StackPanel> 
      </StackPanel> 
     </Border> 
    </DataTemplate> 
    </ListView.ItemTemplate> 
</ListView> 

C#代碼隱藏:

public MainWindow() 
{ 
    InitializeComponent(); 
    FillListView(); 
} 

private void FillListView() 
{ 
    List<MChiStructure> coll = new List<MChiStructure>(); 
    for (int start = 0; start < 10; start++) 
    { 
     coll.Add(new MChiStructure() 
     { 
      TitleField = "Your Title: " + start.ToString(), 
      chiV1Minus = start - 1, 
      chiV1Plus = start + 1, 
      mV1Plus = start - 1 
     }); 
    } 
    listView.ItemsSource = coll; 
} 

型號:

public class MChiStructure 
{ 
    public string TitleField { get; set; } 

    public double chiV1Plus { get; set; } 

    public double chiV1Minus { get; set; } 

    public double mV1Plus { get; set; } 
} 

它看起來像這樣:

enter image description here

+0

這個解決方案是否顯示變量名和它的值到網格中?它似乎只顯示TitleField和所有值...我需要每個值都顯示其名稱 – FrancescoDS

+0

@FrancescoDS請,請參閱我的更新回答 – StepUp

+0

這不完全是我所需要的。我想看到如下行:chiv1plus 1; chiv2plus 0; chiv3plus 2; ...所以每行必須有第一個單元格中的對象字段的名稱和第二個單元格中字段的值 – FrancescoDS

相關問題