2010-05-12 35 views
5

Tôi muốn có một biên giới xung quanh ListViewItem (hàng trong trường hợp của tôi). ListView nguồn và cột được tạo ra trong thời gian chạy. Trong XAML tôi có cấu trúc này:Làm thế nào tôi có thể thêm đường viền vào ListViewItem, ListView trong chế độ GridView

<ListView Name="listViewRaw"> 
    <ListView.View> 
     <GridView> 
     </GridView> 
    </ListView.View> 
</ListView> 

Trong Runtime i ràng buộc listview để DataTable, thêm các cột cần thiết và bindings:

 var view = (listView.View as GridView); 
     view.Columns.Clear(); 
     for (int i = 0; i < table.Columns.Count; i++) 
     { 
      GridViewColumn col = new GridViewColumn(); 
      col.Header = table.Columns[i].ColumnName; 
      col.DisplayMemberBinding = new Binding(string.Format("[{0}]", i.ToString())); 
      view.Columns.Add(col); 
     } 

     listView.CoerceValue(ListView.ItemsSourceProperty); 

     listView.DataContext = table; 
     listView.SetBinding(ListView.ItemsSourceProperty, new Binding()); 

Vì vậy, tôi muốn thêm đường viền bao quanh mỗi hàng, và hành vi biên giới set (màu vv) với DataTriggers (ví dụ: nếu giá trị trong cột 1 = "Có thể nhìn thấy", hãy đặt màu đường viền thành màu đen). Tôi có thể đặt đường viền qua DataTemplate trong ItemTemplate không? Tôi biết giải pháp, nơi bạn thao tác với CellTemplates, nhưng tôi không thực sự thích nó. Tôi muốn một cái gì đó như thế này nếu điều này thậm chí có thể.

<DataTemplate> 
    <Border Name="Border" BorderBrush="Transparent" BorderThickness="2"> 
     <ListViewItemRow><!-- Put my row here, but i ll know about table structure only during runtime --></ListViewItemRow> 
    </Border> 
</DataTemplate> 

Trả lời

11

Bạn sẽ phải thiết lập biên giới của bạn trong ControlTemplate

<Style x:Key="BorderedItem" TargetType="ListViewItem"> 
    <Setter Property="Template"> 
    <Setter.Value> 
     <ControlTemplate TargetType="ListViewItem"> 
     <Border Name="Border" BorderBrush="Transparent" BorderThickness="2"> 
      <ContentPresenter /> 
     </Border> 
     </ControlTemplate> 
    </Setter.Value> 
    </Setter> 
</Style> 

Bây giờ bạn có thể thiết lập phong cách này trong ListView bạn

<ListView ItemContainerStyle="{StaticResource BorderedItem}" /> 
+1

Cảm ơn bạn! Tôi đã thử nghiệm trên DataTemplate theo nhiều cách, tất cả những gì tôi phải làm là chuyển nó sang ControlTemplate, một điều nữa, với GridView, nên sử dụng GridViewRowPresenter thay vì :) – Andrew

10

Giả sử bạn đang sử dụng một ListView với một GridView được đặt làm Chế độ xem, khi đó, ListView không hiển thị đường thẳng đứng hoặc đường ngang theo mặc định.

Nếu bạn muốn thêm dòng horitzontal sau đó bạn có thể thay đổi biên giới trên ListViewItem, ví dụ:

<ListView ...> 
    <ListView.ItemContainerStyle> 
     <Style TargetType="{x:Type ListViewItem}"> 
      <Setter Property="BorderBrush" Value="LightGray" /> 
      <Setter Property="BorderThickness" Value="0,0,0,1" /> 
     </Style> 
    </ListView.ItemContainerStyle> 
    <ListView.View> 
     <GridView> 
      <GridViewColumn ... /> 
     </GridView> 
    </ListView.View> 
    ... 
+1

"ở đây"? Đây là đâu? –

Các vấn đề liên quan