.net 如何在 GridView 模式下向 ListViewItem、ListView 添加边框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2816731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 14:17:20  来源:igfitidea点击:

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

.netwpflistview

提问by Andrew

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:

我想在 ListViewItem 周围有一个边框(在我的例子中是行)。在运行时生成的 ListView 源和列。在 XAML 我有这个结构:

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

所以我想在每一行周围添加边框,并使用 DataTriggers 设置边框行为(颜色等)(例如,如果第一列中的值 =“可见”,则将边框颜色设置为黑色)。我可以通过 ItemTemplate 中的 DataTemplate 放置边框吗?我知道解决方案,您可以在其中使用 CellTemplates 进行操作,但我真的不喜欢它。如果可能的话,我想要这样的东西。

<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>

回答by Tiago S

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.

假设您使用的 ListView 将 GridView 设置为 View,则默认情况下 ListView 不会显示垂直或水平线。

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

如果要添加水平线,则可以更改 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>
    ...

回答by Amsakanna

You'll have to set your border in the ControlTemplate

您必须在 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>

Now you can set this style in your ListView

现在您可以在 ListView 中设置此样式

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