Is there a way to align text vertically (i.e. centered) inside a wpf button created at run time,I’ve tried:
Code Snippet
Button btn = new Button();
Style style = this.FindResource("ButtonStyle1") as Style;
btn.Style = style;
TextBlock txtTblNme = new TextBlock();
txtTblNme.Text = Convert.ToString(Tbl.SelTblNme(i));
txtTblNme.TextAlignment = TextAlignment.Center;
txtTblNme.VerticalAlignment = VerticalAlignment.Center;
txtTblNme.HorizontalAlignment = HorizontalAlignment.Stretch;
btn.Content = txtTblNme;
Look at the ContentAlignment properties. The default of HorizontalContentAlignment and VerticalContentAlignment on the Button should be Center already. Try setting these to Center if they are not already, or if the Style you’re applying above overrides these defaults, you could change them there.
Creating a button at runtime, in XAML, or code-behind should not change the style within your application if the style was created to Target all Buttons (IE: The Style has a TargetType=”{x:Type Button}” and should omit the x:Key attribute). If you’ve specified one with a key, you’ll need to reference in the style as you have in your example above.
Does this help?