如何将单个数据网格行 FontWeights 更改为粗体?
How to change a single datagrid row FontWeights to Bold?
当在我的数据网格中选择一行并按下一个按钮时,我想将该行中单元格的 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;
}
您可以如下定义 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>
DataGrid 将定义为
<DataGrid RowStyle="{StaticResource MyRowStyle}" ...... />
现在要集成它,您必须在绑定到 ItemSource
或 DataGrid
的模型中定义 属性(该模型应实现 INotifyPropertyChanged 接口)。单击按钮时,设置在模型中定义并绑定在 DataTrigger
中的 属性
private void btnConnect_Click(object sender, RoutedEventArgs e)
{
var dataContext = btnConnect.DataContext as <<Your Model>>;
dataContext.IsProcessed = true;
}
事实证明,您可以获得一行这样的数据网格:
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;
当在我的数据网格中选择一行并按下一个按钮时,我想将该行中单元格的 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;
}
您可以如下定义 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>
DataGrid 将定义为
<DataGrid RowStyle="{StaticResource MyRowStyle}" ...... />
现在要集成它,您必须在绑定到 ItemSource
或 DataGrid
的模型中定义 属性(该模型应实现 INotifyPropertyChanged 接口)。单击按钮时,设置在模型中定义并绑定在 DataTrigger
private void btnConnect_Click(object sender, RoutedEventArgs e)
{
var dataContext = btnConnect.DataContext as <<Your Model>>;
dataContext.IsProcessed = true;
}
事实证明,您可以获得一行这样的数据网格:
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;