2016-07-29 69 views
0

假設我想在ListView的一個特定行上顯示信息(在這裏水平顯示,所以它在技術上是列)來模擬日曆般的視覺信息。WPF - 如何明確設置ListView中的項目的索引?

Result structure

第一行天數(月1日至31日)的列表。

另外兩行是來自兩個不同的CSV導出的數字,我比較了同一天的兩個數字。

我使用第一個ListView保持靜態的日期,並顯示一個整數列表。 我正在使用另一個包含「DayData」模型的ListView。

public class DayData 
{ 
    // Top number displayed 
    public float topNumber { get; set; } 

    // Bottom number displayed 
    public float bottomNumber { get; set; } 

    // Matching day for this entry 
    public int matchingDay { get; set; } 

    // Color used to alert user in case of bad ratio 
    public string color { get; set; } 

    /// <summary> 
    /// Initializes a new instance of the <see cref="T:System.Object"/> class. 
    /// </summary> 
    public DayData() 
    { 
     this.topNumber = -1; 
     this.bottomNumber = -1; 
    } 
} 

現在我的問題是,當相比,編號爲空的那一天,我要在列表中顯示「空」的條目,或至少顯示某種利潤率的正確對齊的數字,使在DayData適合他們匹配天數時,就像這樣:

Result wanted

你可能建議增加一個「空」進入名單,但是這會導致性能問題,作爲另一個的ListView承載整個事情,它本身一次顯示幾個ListViews +其他東西的條目。 但是,當我僅顯示DayData條目時沒有任何問題,因爲ListView在一個月內永遠不會包含超過20個。

是否有任何解決方法,就像使用數據模板的網格控件,我可以自己設置行和列並將它們綁定到DayData模型以正確對齊它們?

在此先感謝。

+0

是否有您不使用列的具體原因? – Gusdor

+0

你是什麼意思?我不能使用ListView中的列。沒有任何列的概念,也沒有任何行。只有自動堆疊的「項目」。 –

+1

自己'ListView'的行爲很像一個'Listbox'。但是,您可以通過更改'View'屬性來隨意更改項目的表示形式。開箱即用的實現是'GridView',它可以讓你使用列http://www.wpf-tutorial.com/listview-control/listview-with-gridview/ – Gusdor

回答

0

這就是你如何使用列的每個項目,這將使問題消失,因爲你可以調整列的大小。列表視圖:

<ListView x:Name="testCaseView" HorizontalAlignment="Left" Height="737" Margin="0,182,0,0" VerticalAlignment="Top" Width="1914" FontFamily="Eras ITC" Foreground="Black"> 
     <ListView.View> 
      <GridView> 
       <GridViewColumn DisplayMemberBinding="{Binding testCase}" Header="Test Case" Width="auto"/> 
       <GridViewColumn DisplayMemberBinding="{Binding condition}" Header="Condition" Width="auto"/> 
       <GridViewColumn DisplayMemberBinding="{Binding description}" Header="Description" Width="auto"/> 
       <GridViewColumn DisplayMemberBinding="{Binding expectedResults}" Header="Expected Results" Width="auto"/> 
      </GridView> 
     </ListView.View> 
    </ListView> 

C#創建持有行的所有領域的項目:

public class TestCaseItem 
{ 
    public String testCase { get; set; } 
    public String condition { get; set; } 
    public String description { get; set; } 
    public String expectedResults { get; set; } 
} 

然後填充列表視圖:

testCaseView.Items.Add(new TestCaseItem { testCase = "", condition = "", description = "", expectedResults = "" }); 
+0

事情是我不能在xaml中手動定義它們。我應該可以根據天數列表的大小以編程方式添加它們(一個月中的天數可能會有所不同)。 –

+0

你想每個項目每天都有一個單獨的列嗎? –

+0

是的,看看原帖中的截圖。 –