[英]Setting Label / TextBox HorizontalContentAlignment not working
[英]Setting HorizontalContentAlignment from string
我正在從字符串格式化列標題,例如背景屬性:
var style = new Style(typeof(System.Windows.Controls.Primitives.DataGridColumnHeader));
style.Setters.Add(new Setter(DataGridColumnHeader.BackgroundProperty, new SolidColorBrush((Color)ColorConverter.ConvertFromString("Yellow"))));
但是我不知道如何將HorizontalContentAlignment設置為“ Center”。 我想通過反思,但我不知道在這種情況下的確切使用。
您可以使用Enum.Parse將字符串"Center"
轉換為HorizontalAlignment
類型的相應值:
var style = new Style(typeof(System.Windows.Controls.Primitives.DataGridColumnHeader));
var value = Enum.Parse(typeof(HorizontalAlignment), "Center");
var setter = new Setter(Control.HorizontalContentAlignmentProperty, value);
style.Setters.Add(setter);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.