我正在使用Wpf应用程序。我为wpf DataGrid
创建了自定义样式(在Wpf Toolkit中提供)。除了无法在双击Style
中的单元格(可编辑模式)时出现的TextBox
上应用DataGridTextColumn
之外,其他所有功能均正常运行。它显示为默认样式,与我的样式不匹配,看起来很奇怪。我已在ComboBox
和DataGridComboBoxColumn
中的CheckBox
和所有其他控件上应用了一种样式,但是此样式无法正常工作。任何帮助请!!
我有一个控件库,每个控件都在此处被覆盖以进行自定义(附加功能)和重新样式化。这些控件将在整个应用程序中使用。我必须将此样式应用于控件库中的控件。这样我就可以在整个应用程序中体现它。
不完美,但是可以工作...
<Style x:Key="DataGridTextBoxStyle"
TargetType="TextBox">
<Setter
Property="SelectionBrush"
Value="#FFF8D172" />
<Setter
Property="Padding"
Value="0" />
<Setter
Property="VerticalContentAlignment"
Value="Center" />
<Setter
Property="FontSize"
Value="9pt" />
<Setter
Property="SelectionOpacity"
Value="0.6" />
</Style>
<DataGridTextColumn
x:Name="TextColumn"
Header="Header"
EditingElementStyle="{StaticResource ResourceKey=DataGridTextBoxStyle}"/>
这也可以通过PreparingCellForEdit
的DataGrid
事件来实现,如果您不想覆盖系统EditingElementStyle
,或者使用AutoGenerateColumns
,或者当您有多列并且不能可以单独设置它们。
private void DataGrid_PreparingCellForEdit(object sender,
DataGridPreparingCellForEditEventArgs e)
{
if (!(e.EditingElement is TextBox textBox))
return;
// or from resources - Application.Current.FindResource etc.
var style = new Style(typeof(TextBox), textBox.Style);
style.Setters.Add(new Setter { Property = ForegroundProperty, Value = Brushes.Red });
textBox.Style = style;
}