質問

かわ列のデータセンター、コンポーネントのラインナップ 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>

他のヒント

あなたはDataGridTextColumnを使用している場合は、次のコードスニペットを使用することができます:

<Style TargetType="DataGridCell">
     <Style.Setters>
            <Setter Property="TextBlock.TextAlignment" Value="Center" />
     </Style.Setters>
</Style>

ただhuttelihutいます。残念ながら、動作しなかった私にとってだけます。I調整えたこと(ソリューションを揃え、テキストの右)

<Resources>
    <Style x:Key="RightAligned" TargetType="TextBlock">
        <Setter Property="HorizontalAlignment" Value="Right"/>
    </Style>
</Resources>

ご覧の通り、私のスタイルをTextBlockではなく、DataGridCell.

そのための設定 要素 スタイルではなく、 細胞 ます。

ElementStyle="{StaticResource RightAligned}"
ケントBoogaartのための

1。 私は少しすっきりコードを作る(および複数の列に整列を使用するように私を可能にします)が、これをやってしまった。

<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>

ここでMohammedAFadilのXAMLの答えは、@だ、背後にあるC#コードに変換します:

var MyStyle = new Style(typeof(DataGridCell)) {
    Setters = {
        new Setter(TextBlock.TextAlignmentProperty, TextAlignment.Center)
    }
};

Styleを適用するには、例えば、CellStyleDataGridプロパティを設定します。

var MyGrid = new DataGrid() {
    CellStyle = MyStyle
};

それとも背後にあるコードでます:

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;
}

私は、セルに問題がシフトされたと受け入れ答えを使用してファンキー見てしまいました。私はそれが後半だけど、うまくいけば私の調査結果は、誰かを助けます。私が使用します:

<DataGridTextColumn.ElementStyle>
     <Style>
          <Setter Property="FrameworkElement.HorizontalAlignment" Value="Center"/>
     </Style>

というよりもCellStyle。

[OK]を、私はFrameworkElementのアプローチを使用していますが、行を強調しようとする奇妙な行動がありました。

私はこの中でWPFデータグリッドアライメントの別の例を入れている<のhref =「https://stackoverflow.com/questions/1050536/highlight-entire-rows-only-in-silverlight-datagrid/1324343#1324343」 >スレッドで!

私のお気に入りのソリューションは、次のとおりです。

<DataGridTextColumn Header="My Column" Binding="{Binding MyDBValue}" Width="100" >
<DataGridTextColumn.CellStyle>
        <Style>
                <Setter Property="FrameworkElement.HorizontalAlignment" Value="Center"/>
        </Style>
</DataGridTextColumn.CellStyle>

おかげでダニー・ベケットMohammedAFadilのXAMLの答え@変換するための、C#コードに変換します。私のデータグリッドのすべては、動的に設定されているので、私はいつでも、何も変更することができます。

はそれで何もして、空のデータグリッドを設定してからちょうどちょうどあなたのdatagrid.columnsを取る、それをデータにバインドするには、

        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つがうまく機能

<DataGridTextColumn Width="1*" Binding="{Binding Balance, StringFormat=C} "Header="Balance">
  <DataGridTextColumn.CellStyle>
      <Style>
        <Setter Property="TextBlock.TextAlignment"  Value="Right"/>
      </Style>
   </DataGridTextColumn.CellStyle>
 </DataGridTextColumn>
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top