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

I want to have a border around ListViewItem (a string in my case). ListView source and columns created at runtime. In XAML, I have this structure:

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

At run time, I bind a listview to a DataTable, adding the 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 a border around each row and set the border behavior (color, etc.) using DataTriggers (for example, if the value in the 1st column = "Visible", set the border color to black). Can I place a border through a DataTemplate in an ItemTemplate? I know a solution where you are manipulating with CellTemplates, but I don't really like it. I want something like this if 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>
+5
2

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>

ListView

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

, ListView GridView, , ListView .

, ListViewItem, :

<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>
    ...
+11

All Articles