【问题标题】:Style datagrid table - Top left corner样式数据网格表 - 左上角
【发布时间】:2011-11-30 15:24:25
【问题描述】:

我正在为数据表设置样式,但我不知道如何设置数据网格左上角的样式。就是这张图中的灰色区域:

你知道怎么做吗?

这是我目前的风格:

<Style TargetType="{x:Type DataGrid}">
    <Setter Property="Margin" Value="5" />
    <Setter Property="Background">
        <Setter.Value>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="White"/>
                <GradientStop Color="AliceBlue" Offset="1"/>
            </LinearGradientBrush>
        </Setter.Value>
    </Setter>
    <Setter Property="RowBackground">
        <Setter.Value>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="#BAF0FF"/>
                <GradientStop Color="PowderBlue" Offset="1"/>
            </LinearGradientBrush>
        </Setter.Value>
    </Setter>
    <Setter Property="AlternatingRowBackground">
        <Setter.Value>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="White"/>
                <GradientStop Color="AliceBlue" Offset="1"/>
            </LinearGradientBrush>
        </Setter.Value>
    </Setter>
    <Setter Property="HorizontalGridLinesBrush" Value="LightGray" />
    <Setter Property="VerticalGridLinesBrush" Value="LightGray" />
</Style>

【问题讨论】:

  • 您的 DataGrid 中是否需要 RowHeaders?如果没有,我建议完全删除它们(在&lt;DataGrid&gt; 标签上设置HeadersVisibility="Column"),这样角单元格就不会显示。

标签: c# wpf coding-style datagrid


【解决方案1】:

通过answer,我能够创建正确设置按钮样式的代码:

<DataGrid>    
    <DataGrid.Resources>
        <Style TargetType="Button" x:Key="{ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, TypeInTargetAssembly={x:Type DataGrid}}">
            <Setter Property="Background" Value="Black" />
        </Style>
    </DataGrid.Resources>
</DataGrid>

【讨论】:

    【解决方案2】:

    我得到了一个不完美但可行的解决方案。
    您可以通过 VisualTreeHelper 获取数据网格的“左上角”对象。 这实际上是一个按钮。我想你知道下一步该怎么做。
    这是我的工作代码:

    //Change the top left button to a CheckBox
    void StyleSelectAllButton(DependencyObject dependencyObject)
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(dependencyObject); i++)
        {
            var child = VisualTreeHelper.GetChild(dependencyObject, i);
            if ((child != null) && child is Button)
            {
                var grid = (Grid)VisualTreeHelper.GetChild(child, 0);
    
                var checkBox = new CheckBox()
                {
                    VerticalAlignment = VerticalAlignment.Center,
                    HorizontalAlignment = HorizontalAlignment.Center,
                };
    
                checkBox.Click += OnCheckBoxClicked;
    
                grid.Children.Clear();
                grid.Children.Add(checkBox);
            }
            else
            {
                StyleSelectAllButton(child);
            }
        }
    }
    
    //Action when the top left check box checked and unchecked
    void OnCheckBoxClicked(object sender, RoutedEventArgs e)
    {
        var checkBox = sender as CheckBox;
    
        if (checkBox == null)
        {
            return;
        }
    
        if (checkBox.IsChecked == true)
        {
            //Change the 'dataGrid' to your DataGrid instance name
           dataGrid.SelectAllCells(); 
        }
        else
        {
            //Change the 'dataGrid' to your DataGrid instance name
            dataGrid.UnselectAllCells();
        }
    }
    

    【讨论】:

      【解决方案3】:

      无法让 Gman 的解决方案发挥作用,但能够使用代码隐藏中的这一行隐藏左上角(添加到 Loaded 事件中):

      (DataGridPricing.FindVisualChild<Button>()).Opacity = 0;
      

      这只是找到对象 - 然后通过使其不可见来解决问题(对我而言)。但是,在改变颜色方面没有任何运气。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-26
        • 2010-10-07
        • 2014-06-11
        • 1970-01-01
        • 2017-08-20
        相关资源
        最近更新 更多