3

私はWpfアプリケーションを使用しています。wpfのカスタムスタイルを作成しましたDataGrid(Wpf Toolkitで提供されています)。のセル(編集可能モード)をダブルクリックすると表示されるStyleにを適用できないことを除いて、すべて正常に機能します。デフォルトのスタイルとして表示されますが、私のスタイルと一致せず、奇妙に見えます。in、および、および他のすべてのコントロールにスタイルを適用しましたが、これは機能していません。任意のヘルプplz!!!TextBoxDataGridTextColumnComboBoxDataGridComboBoxColumnCheckBox

編集:

私はコントロールライブラリを持っており、カスタマイズ(追加機能)とスタイル変更のためにすべてのコントロールがここでオーバーライドされます。これらのコントロールは、アプリケーション全体で使用されます。このスタイルをコントロールライブラリのコントロールに適用する必要があります。アプリケーション全体に反映させるためです。

4

2 に答える 2

5

完璧ではありませんが、機能します...

<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}"/>
于 2011-03-21T23:01:19.507 に答える
0

これは、システムをオーバーライドしたくない場合、またはを使用する場合、または複数の列があり、それらを個別に設定する余裕がない場合のPreparingCellForEditイベントでも実現できます。DataGridEditingElementStyleAutoGenerateColumns

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;
}
于 2019-12-10T02:50:28.880 に答える