C# 如何将单个datagrid行更改为粗体?

C# 如何将单个datagrid行更改为粗体?,c#,wpf,wpfdatagrid,C#,Wpf,Wpfdatagrid,当在我的datagrid中选择一行并按下按钮时,我想将该行中单元格的FontWeight更改为粗体 我一直在寻找一种方法,但我所能做的就是更改每一列的样式,我找不到一种方法来获取所选行(或任何行) 没有可以从ItemSource类型绑定到的特定值,因此由于复杂性增加,不需要使用XAML和ValueConverter的解决方案。也就是说,除非这是唯一的办法 我就是这样继续的: <DataGrid Name="dgSessions" Width="200" Height="100"

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

我一直在寻找一种方法,但我所能做的就是更改每一列的样式,我找不到一种方法来获取所选行(或任何行)

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

我就是这样继续的:

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


    }

私有void btn连接\u单击(对象发送方,路由目标)
{
Style oldStyle=dgSessions.SelectedCells.First().Column.CellStyle;
Setter Setter=null;
foreach(旧样式的Setter项。Setters)
{
如果(item.Property.Name==“FontWeight”)
{
setter=新的setter(item.Property,FontWeights.Bold,item.TargetName);
打破
}
}
样式newStyle=新样式(oldStyle.TargetType);
newStyle.Setters.Add(setter);
dgSessions.SelectedCells.First().Column.CellStyle=newStyle;
}

您可以如下定义
DataGridRow
样式,然后单击按钮“设置属性”触发通知,在行上应用
FontWeight

<Style x:Key="MyRowStyle" TargetType="DataGridRow">
    <Setter Property="FontWeight" Value="Normal"/>
    <Style.Triggers>
        <DataTrigger Binding="{Binding IsProcessed}" Value="True">
            <Setter Property="FontWeight" Value="Bold"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

事实证明,您可以得到一行数据网格,如下所示:

DataGridRow row = (DataGridRow)myDataGrid.ItemContainerGenerator.ContainerFromIndex(myIndex);
还有一种不同的方法可以从项目中获取行

因此,我执行了以下操作,以粗体显示我想要的行:

  • 使用
    int index=myObservableCollection.IndexOf(myObject)
    如果你有很多行,我不认为索引总是有效的 虚拟化已经启用,但考虑到我的环境,它还可以

  • 创建我的Setter

    Setter bold = new Setter(TextBlock.FontWeightProperty, FontWeights.Bold, null);
    
  • 获取我的行:

    DataGridRow row = (DataGridRow)dgSessions.ItemContainerGenerator.ContainerFromIndex(index);
    
  • 创建样式并进行设置:

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

  • 我不熟悉“模型”的概念。我有一个Session(我自己的类)的ObservableCollection(我的datagrid的itemsSource),其中datagrid的列绑定到Session.model的Name属性这里是您创建的类感谢您到目前为止的帮助。我尝试将bool属性“IsProcessed”添加到模型中,并实现了INotifyPropertyChanged接口。虽然,考虑到它是一个不可变的结构,这有点奇怪。我的按钮只是窗口上的一个按钮,它没有dataContext,因此我执行了以下操作:
    sessions=(Session)dgSessions.SelectedItem;s、 IsProcessed=true但没有更改。
    
    DataGridRow row = (DataGridRow)dgSessions.ItemContainerGenerator.ContainerFromIndex(index);
    
        Style newStyle = new Style(row.GetType());
    
        newStyle.Setters.Add(bold);
        row.Style = newStyle;