如何舍入ListViewItem的角落

时间:2011-04-04 14:15:03

标签: wpf listviewitem

我正在尝试为ListViewItem的高亮和选定背景设置圆角

我有以下代码:

<LinearGradientBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" StartPoint="0,0" EndPoint="0,1">
        <LinearGradientBrush.GradientStops>
            <GradientStopCollection>
                <GradientStop Color="#F7D073" Offset="0"/>
                <GradientStop Color="#F1A62F" Offset="1"/>
            </GradientStopCollection>
        </LinearGradientBrush.GradientStops>
    </LinearGradientBrush>

    <LinearGradientBrush x:Key="{x:Static SystemColors.ControlBrushKey}" StartPoint="0,0" EndPoint="0,1">
        <LinearGradientBrush.GradientStops>
            <GradientStopCollection>
                <GradientStop Color="#F7D073" Offset="0"/>
                <GradientStop Color="#F1A62F" Offset="1"/>
            </GradientStopCollection>
        </LinearGradientBrush.GradientStops>
    </LinearGradientBrush>

    <LinearGradientBrush x:Key="MouseOverBrush" StartPoint="0,0" EndPoint="0,1">
        <LinearGradientBrush.GradientStops>
            <GradientStopCollection>
                <GradientStop Color="#E4F0FD" Offset="0"/>
                <GradientStop Color="#D7EAFD" Offset="1"/>
            </GradientStopCollection>
        </LinearGradientBrush.GradientStops>
    </LinearGradientBrush>

    <Style TargetType="{x:Type ListViewItem}">
        <Setter Property="Foreground">
            <Setter.Value>
                <SolidColorBrush Color="{Binding Path=NGGDataForeground, ElementName=NGG}" />
            </Setter.Value>
        </Setter>
        <Setter Property="FontWeight" Value="Normal" />
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="true">
                <Setter Property="Background" Value="{StaticResource MouseOverBrush}" />
            </Trigger>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Foreground" Value="Black" />
            </Trigger>
        </Style.Triggers>
        <Style.Resources>
            <Style TargetType="Border">
                <Setter Property="CornerRadius" Value="2"/>
            </Style>
        </Style.Resources>
    </Style>
<ListView x:Name="lstData" ItemsSource="{Binding ElementName=NGG}" 
          Height="{Binding Path=GridHight, ElementName=NGG}" 
          Width="{Binding Path=GridWidth, ElementName=NGG}" 
          BorderThickness="0" BorderBrush="Transparent">
</ListView>

然而由于某些原因,这只绕左侧而不是右侧寻找有关如何围绕所有4个角的帮助。

2 个答案:

答案 0 :(得分:2)

如果我在新窗口上使用上述XAML,它对我来说很好用:

enter image description here

ListView的右侧是否可能无法按预期显示?也许绑定的GridWidth比放置它的控件稍大一点。

答案 1 :(得分:0)

我的列是在

背后的代码中动态创建的
public void PopulateGrid(DataSet ds) {
        lstData.View = CreateGridViewColumns(ds.Tables[0]);
        lstData.ItemsSource = ((IListSource) ds.Tables[0]).GetList();
    }

    public void PopulateGrid(DataTable dt) {
        lstData.View = CreateGridViewColumns(dt);
        lstData.DataContext = dt.DefaultView;
    }

    private GridView CreateGridViewColumns(DataTable dt) {
        // Create the GridView
        var gv = new GridView {AllowsColumnReorder = true};

        // Create the GridView Columns
        foreach (DataColumn item in dt.Columns) {
            var gvc = new GridViewColumn
                          {
                              DisplayMemberBinding = new Binding(item.ColumnName),
                              Header = item.ColumnName,
                              Width = Double.NaN
                          };

            gv.Columns.Add(gvc);
        }

        return gv;
    }

我也注意到,如果我展开最后一列以显示水平滚动条,并且它有一个垂直条,那么右侧角就会显示出来。

此外,如果我手动定义所有列,那么圆角全部显示在所有4个边上(这不是理想的,我需要列表视图是动态的)