C# 如何重新设置DataGridTextColumn的TextBox的样式?

C# 如何重新设置DataGridTextColumn的TextBox的样式?,c#,wpf,xaml,styles,datagridtextcolumn,C#,Wpf,Xaml,Styles,Datagridtextcolumn,我正在使用Wpf应用程序。我已经为wpfDataGrid(在wpf工具包中提供)创建了一个自定义样式。除了我无法在双击DataGridTextColumn中的单元格(可编辑模式)时在TextBox上应用Style之外,一切正常。它显示为默认样式,与我的样式不匹配,看起来很奇怪。我已经在datagridcomboxcolumn中的组合框和复选框以及所有其他控件上应用了一种样式,但是这个样式不起作用。任何帮助plz 编辑: 我有一个控件库,每个控件都在这里被覆盖,用于定制(附加功能)和重新设置样式。

我正在使用Wpf应用程序。我已经为wpf
DataGrid
(在wpf工具包中提供)创建了一个自定义样式。除了我无法在双击
DataGridTextColumn
中的单元格(可编辑模式)时在
TextBox
上应用
Style
之外,一切正常。它显示为默认样式,与我的样式不匹配,看起来很奇怪。我已经在
datagridcomboxcolumn
中的
组合框
复选框
以及所有其他控件上应用了一种样式,但是这个样式不起作用。任何帮助plz

编辑:
我有一个控件库,每个控件都在这里被覆盖,用于定制(附加功能)和重新设置样式。这些控件在整个应用程序中使用。我必须在控件库中的控件上应用此样式。这样我就可以在我的整个应用程序中反映出来。

不完美,但很有效

<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}"/>

如果您不想覆盖系统
EditingElementStyle
,或者如果使用
AutoGenerateColumns
,或者当您有多个列且无法单独设置时,也可以通过
DataGrid
PreparingCellForEdit
事件来实现

private void DataGrid_PreparingCellForEdit(object sender, 
  DataGridPreparingCellForEditEventArgs e)
{
  if (!(e.Column is DataGridTextColumn && e.EditingElement is TextBox textBox))
    return;

  var style = new Style(typeof(TextBox), textBox.Style);        
  style.Setters.Add(new Setter { Property = ForegroundProperty, Value = Brushes.Red });
  textBox.Style = style;      
}
如果要应用应用程序资源:

private void DataGrid_PreparingCellForEdit(object sender, 
  DataGridPreparingCellForEditEventArgs e)
{
  if (!(e.Column is DataGridTextColumn && e.EditingElement is TextBox textBox))
    return;

  var tbType = typeof(TextBox);
  var resourcesStyle = Application
    .Current
    .Resources
    .Cast<DictionaryEntry>()
    .Where(de => de.Value is Style && de.Key is Type styleType && styleType == tbType)
    .Select(de => (Style)de.Value)
    .FirstOrDefault();

  var style = new Style(typeof(TextBox), resourcesStyle);
  foreach (var setter in textBox.Style.Setters)
    style.Setters.Add(setter);

  textBox.Style = style;
}
private void DataGrid\u PreparingCellForEdit(对象发送方,
DataGridPreparingCellForeditEventTargets(e)
{
如果(!(e.Column是DataGridTextColumn&&e.EditingElement是TextBox TextBox))
返回;
var tbType=typeof(文本框);
var resourcesStyle=应用程序
现在的
.资源
.Cast()
.Where(de=>de.Value是Style&&de.Key是Type-styleType&&styleType==tbType)
.选择(de=>(样式)de.Value)
.FirstOrDefault();
var style=新样式(typeof(TextBox),resourcesStyle);
foreach(textBox.Style.Setters中的var setter)
style.setter.Add(setter);
Style=Style;
}

也许我误解了这个问题,您是否有与我的示例类似但不起作用的内容?在这种情况下,你有一些示例代码吗?更新了我的答案,但我不完全理解你想做什么。您可以添加一些您已经尝试过的示例代码吗?