列データをWPF DataGrid
の中央に揃えるにはどうすればよいですか?WPF DataGridでのテキストの配置
答えて
それは詳細を知らずに言うのは難しいのですが、ここでは中央に配置されDataGridTextColumn
です:
<wpf:DataGridTextColumn Header="Name" Binding="{Binding Name}" IsReadOnly="True">
<wpf:DataGridTextColumn.CellStyle>
<Style>
<Setter Property="FrameworkElement.HorizontalAlignment" Value="Center"/>
</Style>
</wpf:DataGridTextColumn.CellStyle>
</wpf:DataGridTextColumn>
2010年12月にも私のために働きました! – ErocM
このアプローチは私のためには機能しませんでした。テキストは中央揃えされていますが、セルの幅はヘッダーと一致しなくなりました。モハメッドのソリューションが最も効果的でした。 –
Kiffが言ったように、細胞も動いています。 Mohammed A. Fadilの例文のようにTextblockをターゲットにする必要があります。 –
[OK]を、私はFrameworkElementのアプローチを使用していますが、行を強調しようとすると、奇妙な行動がありました。
私はこのthreadにWPF Datagridアライメントの別の例を載せました!ケントBoogaartのため
<Style TargetType="DataGridCell">
<Style.Setters>
<Setter Property="TextBlock.TextAlignment" Value="Center" />
</Style.Setters>
</Style>
次のコードスニペットを使用することができます。
<Resources>
<Style x:Key="NameCellStyle" TargetType="DataGridCell">
<Setter Property="HorizontalAlignment" Value="Center" />
</Style>
</Resources>
<DataGrid.Columns>
<DataGridTextColumn Header="Name" CellStyle="{StaticResource NameCellStyle}" Binding="{Binding Name}"/>
// .. other columns
</DataGrid.Columns>
これは、セルの整列を設定する最善の方法です。行が選択されたときに奇妙に見えるセルの幅がセルに埋め込まれないようにします。 – bruceboughton
最初はこれは機能しませんでしたが、このソリューションは素晴らしいコードでした。ありがとう! – BrianVPS
@BrianVPS歓迎、私はそれを聞いて満足している:D –
+1:あなたはDataGridTextColumnを使用している場合
+1スタイルでは、私の場合は 'HorizontalAlignment'は動作しませんが、' HorizontalContentAlignment'は動作します。 – Emmanuel
私のお気に入りのソリューションは次のとおりです:
<DataGridTextColumn Header="My Column" Binding="{Binding MyDBValue}" Width="100" >
<DataGridTextColumn.CellStyle>
<Style>
<Setter Property="FrameworkElement.HorizontalAlignment" Value="Center"/>
</Style>
</DataGridTextColumn.CellStyle>
私は少しすっきりコードを作る(および複数の列に整列を使用するように私を可能にします)が、これをやってしまいましたコードの後ろに:
grid.CellStyle = newCellStyle();
public static Style newCellStyle()
{
//And here is the C# code to achieve the above
System.Windows.Style style = new Style(typeof(DataGridCell));
style.Setters.Add(new System.Windows.Setter
{
Property = Control.HorizontalAlignmentProperty,
Value = HorizontalAlignment.Center
});
return style;
}
私はhuttelihutのソリューションから始めました。残念ながら、それはまだ私にとってはうまくいきませんでした。私は彼の答えを微調整し、この(溶液が右にテキストを揃えることである)を思い付いた:あなたが見ることができるように
<Resources>
<Style x:Key="RightAligned" TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Right"/>
</Style>
</Resources>
、私はTextBlockのではなく、DataGridCellにスタイルを適用します。
そして私は要素スタイルではなく、セルスタイルを設定する必要がありました。
ElementStyle="{StaticResource RightAligned}"
これは 'DataGridTextColumn'で動作しませんのでご注意ください –
@JP Hellemons:私にとっては' DataGridTextColumn'で動作しました – Mikhail
私は、セルに問題がシフトされたと受け入れ答えを使用してファンキー見てしまいました。私はそれが遅れていることを知っているが、うまくいけば私の発見は誰かを助けるだろう。私が使用しますというよりCellStyle
<DataGridTextColumn.ElementStyle>
<Style>
<Setter Property="FrameworkElement.HorizontalAlignment" Value="Center"/>
</Style>
を。
ここでの背後にあるC#コードに変換し、MohammedAFadilのXAMLの答えは@です:
var MyStyle = new Style(typeof(DataGridCell)) {
Setters = {
new Setter(TextBlock.TextAlignmentProperty, TextAlignment.Center)
}
};
例えば、Style
を適用DataGrid
のCellStyle
プロパティを設定するには
var MyGrid = new DataGrid() {
CellStyle = MyStyle
};
シンプルでクリーンです! +1 :)ちょうど私が必要なもの。 –
おかげでダニー・ベケットMohammedAFadilのXAMLの答え@変換するための、C#コードに変換します。私のすべてのデータグリッドは動的に設定されているので、いつでも何でも変更できます。
何も入っていない空のDataGridを設定し、それをデータにバインドするには、DataGridを使用します。Silverlightの列
var centerTextSetter = new Style(typeof(DataGridCell))
{
Setters = { new Setter(TextBlock.TextAlignmentProperty, TextAlignment.Center) }
};
DgDbNames.Columns.Add(new DataGridTextColumn()
{
Header = "Db Name",
Binding = new System.Windows.Data.Binding("DbName"),
IsReadOnly = true,
Width = new DataGridLength(0.2, DataGridLengthUnitType.Star),
CellStyle = centerTextSetter
});
- 1. WPF XAML DataGridサブDataGridに配置する
- 2. wpfのdatagrid行の最後にdatagrid列を配置する方法は?
- 3. WPF ComboboxのDataGrid列の配置と一致
- 4. DataGridのテキストのサイズの設定wpf
- 5. WPF Visual C#DataGridのDataGrid
- 6. WPFタブアイテムの配置
- 7. WPFツールチップの配置
- 8. DataGridのWPFイメージバインド
- 9. WPFのDataGridオートサイジング
- 10. DataGridのWPFフィールド
- 11. WPF DataGridのColspan
- 12. WPF DataGridのリゾートデータ
- 13. C#WPFのDataGrid
- 14. テキストの配置
- 15. WPFアプリケーションのトレーアイコンコンテキストメニューの配置
- 16. WPF DataGrid(MVVM)のScrollIntoView
- 17. DataGrid/ListBoxのWPFボタン
- 18. WPF DataGrid - グレーデッドカラースケールのカラーセル
- 19. WPF DataGrid CellEditingTemplateのダブルクリックイベント
- 20. WPF DataGridの背景
- 21. WPF Datagridクロスローのバリデーション
- 22. WPF DataGridの「Invalid」datetime
- 23. WPF DataGridのTextWrappingテキストボックス
- 24. Wpf DataGridの問題
- 25. DataGridへのDbDataReader WPF
- 26. WPF DataGridの「ドラッグ&コピー」
- 27. WPF Datagridコレクションオブジェクトのフラットコレクション
- 28. WPF DataGridの行テンプレート
- 29. WPFの絶対配置
- 30. WPF ListView列ヘッダーの配置
、[この回答](http://stackoverflow.com/questions/7916226/column-aligment-in-a-silverlight-datagrid/7916301#7916301)を参照してください。 –