2013-07-21 57 views
2

如何將畫布放置在ListView控件的特定欄內?如何將畫布放在ListView控件的特定列中?

我有這樣的:

<DataTemplate> 
    <Canvas Width="60" Height="20" Background="Red" ClipToBounds="True" > 
     <ContentPresenter Content="{Binding Path=Graph}" /> 
    </Canvas> 
</DataTemplate> 

和:

var canvas = new Canvas(); 
canvas.Children.Add(new Ellipse(){}); 
var items = new ObservableCollection<LvItem>() { new LvItem(){ Graph = canvas} }; 
myListView.ItemsSource = items; 

但它顯示System.Windows.Controls.Canvas爲文本,而不是畫布本身及其附圖。

回答

2

它的工作,檢查下面的例子。

XAML:

<ListView Name="myListView" > 
    <ListView.View> 
     <GridView> 
      <GridViewColumn Width="140" Header="Grap"> 
       <GridViewColumn.CellTemplate> 
        <DataTemplate> 
         <Canvas Width="60" Height="50" Background="Red" ClipToBounds="True" > 
          <ContentPresenter Content="{Binding Path=Graph}" /> 
         </Canvas> 
        </DataTemplate> 
       </GridViewColumn.CellTemplate> 
      </GridViewColumn>      
     </GridView> 
    </ListView.View> 
</ListView> 

LVITEM類:

class LvItem 
{ 
    public Canvas Graph { get; set; } 
} 

代碼隱藏:

var canvas = new Canvas(); 
canvas.Children.Add(new Ellipse() { Width = 50, Height = 50, Fill = new SolidColorBrush(Colors.Yellow) }); 
var canvas2 = new Canvas(); 
canvas2.Children.Add(new Ellipse() { Width = 50, Height = 50, Fill = new SolidColorBrush(Colors.Blue) }); 
var items = new ObservableCollection<LvItem>() { new LvItem() { Graph = canvas }, new LvItem() { Graph = canvas2 } }; 
myListView.ItemsSource = items; 

結果:

enter image description here

+1

這很令人尷尬......我很久沒有注意到我在'GridViewColumn'屬性中有'DisplayMemberBinding =「{Binding Graph}」'...感謝您確認它確實工作,我專注於找到我的錯誤... – Tar

相關問題