如何将单个datagrid行的FontWeights更改为Bold? [英] How to change a single datagrid row FontWeights to Bold?

查看:84
本文介绍了如何将单个datagrid行的FontWeights更改为Bold?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在数据网格中选择一行并按下按钮时,我想将该行中单元格的FontWeight更改为粗体。

When a row is selected in my datagrid and a button is pressed, I want to change the FontWeight of the cells in that row to bold.

我已经一直在寻找一种方法来做,但是我所能做的就是改变每列的样式,我找不到一种方法来获取选定的行(或与此相关的任何行)。

I've been looking for a way to do it, but all I can do is change the style of every columns, I can't find a way to get the selected row (or any rows for that matter).

我没有可以绑定到ItemSource类型的特定值,因此由于复杂性的增加,不需要使用XAML和ValueConverter的解决方案。也就是说,除非这是唯一的方法。

There are no specific values that I can bind to from the ItemSource type, so a solution using XAML and a ValueConverter is unwanted due to the increased complexity. That is, unless it's the only way.

这就是我要进行的操作:

This is how I'm proceeding:

 <DataGrid Name="dgSessions" Width="200" Height="100" 
                          CanUserAddRows="False" CanUserDeleteRows="False" 
                          HeadersVisibility="None" GridLinesVisibility="None" AutoGenerateColumns="False"
                          SelectionMode="Single" Background="White">
                    <DataGrid.Columns>
                        <DataGridTextColumn Width="*" Binding="{Binding Path=Name}"></DataGridTextColumn>
                    </DataGrid.Columns>
                    <DataGrid.CellStyle>
                        <Style TargetType="{x:Type DataGridCell}">
                            <Style.Setters>
                                <Setter Property="FontWeight"
                                        Value="Normal"/>
                            </Style.Setters>
                        </Style>
                    </DataGrid.CellStyle>
  </DataGrid>


        private void btnConnect_Click(object sender, RoutedEventArgs e)
    {
        Style oldStyle = dgSessions.SelectedCells.First().Column.CellStyle;
        Setter setter = null;
        foreach (Setter item in oldStyle.Setters)
        {
            if (item.Property.Name == "FontWeight")
            {
                setter = new Setter(item.Property, FontWeights.Bold, item.TargetName);
                break;
            }
        }
        Style newStyle = new Style(oldStyle.TargetType);
        newStyle.Setters.Add(setter);
        dgSessions.SelectedCells.First().Column.CellStyle = newStyle;


    }


推荐答案

结果是,您可以得到一行数据网格,如下所示:

Turns out, you can get a row of a datagrid like that:

DataGridRow row = (DataGridRow)myDataGrid.ItemContainerGenerator.ContainerFromIndex(myIndex);

还有一种从项目中获取行的方法。

There is also a different method to get a row from an Item.

所以我执行以下操作以粗体设置了我想要的行:

So I did the following to set the row I want in bold:


  1. 检索索引使用 int index = myObservableCollection.IndexOf(myObject)
    如果您有很多行
    和虚拟化,我认为索引并不总是有效的启用,但考虑到我的上下文,就可以了。

  1. Retrieve the index with int index = myObservableCollection.IndexOf(myObject) I don't think the index is always valid if you have a lot of rows and virtualization is enabled, but given my context, it's fine.

创建我的二传手

Setter bold = new Setter(TextBlock.FontWeightProperty, FontWeights.Bold, null);


  • 获取我的行:

  • Get my row:

    DataGridRow row = (DataGridRow)dgSessions.ItemContainerGenerator.ContainerFromIndex(index);
    


  • 创建样式并进行设置:

  • Create Style and set it:

        Style newStyle = new Style(row.GetType());
    
        newStyle.Setters.Add(bold);
        row.Style = newStyle;
    


  • 这篇关于如何将单个datagrid行的FontWeights更改为Bold?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆