Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How i can add border to ListViewItem, ListView in GridView mode

Tags:

.net

listview

wpf

I want to have a border around ListViewItem (row in my case). ListView source and columns generated during Runtime. In XAML i have this structure:

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

During Runtime i bind listview to DataTable, adding necessary columns and 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());

So i want to add border around each row, and set border behavior (color etc) with DataTriggers (for example if value in 1st column = "Visible", set border color to black). Can i put border through DataTemplate in ItemTemplate? I know solution, where you manipulate with CellTemplates, but i don't really like it. I want something like this if this even possible.

<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>
like image 570
Andrew Avatar asked May 12 '10 07:05

Andrew


People also ask

How to add border in ListView?

For this first take the LinearLayout and assign that linear layout with some color and take a list view in that linear layout. Set the android:layout_margin="10dp" property for list view . That means on all 4 sides 10dp space will be left. This shown as the border of the list view.

What is GridView WPF?

The GridView view mode displays a list of data items by binding data fields to columns and by displaying a column header to identify the field. The default GridView style implements buttons as column headers.


1 Answers

Assuming you're using a ListView with a GridView set as the View, then the ListView doesn't show vertical or horizontal lines by default.

If you want to add horitzontal lines then you can change the border on the ListViewItem, e.g:

<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>
    ...
like image 101
Tiago S Avatar answered Oct 26 '22 05:10

Tiago S