20

Let's say, I've got something like this (in MainPage.xaml):

<Page.Resources>
    <Style TargetType="TextBlock" x:Key="TextBlockStyle">
        <Setter Property="FontFamily" Value="Segoe UI Light" />
        <Setter Property="Background" Value="Navy" />
    </Style>
</Page.Resources>

Then, I would like to apply that StaticResource style to my dynamic created TextBlock (file MainPage.xaml.cs).

Is there any possibility to do this instead of doing something like this:

myTextBlock.FontFamily = new FontFamily("Segoe UI Light");
myTextBlock.Background = new SolidColorBrush(Color.FromArgb(255,0,0,128));
kodi1911
  • 662
  • 1
  • 18
  • 34

4 Answers4

26

It has been more than 4 years now since this question was asked, but I want to post an answer just to share my findings.

For example if there is a Style BlueButton described in Application resource in App.xaml (Xamarin Cross-Platform App development), it can be used as follows

<?xml version="1.0" encoding="utf-8" ?><Application xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="SharedUi.App">
<Application.Resources>
    <ResourceDictionary>
        <Style x:Key="BlueButton" TargetType="Button">
            <Setter Property="TextColor" Value="White" />
            <Setter Property="FontSize" Value="20" />
            <Setter Property="BackgroundColor" Value="Blue"/>
            <Setter Property="HeightRequest" Value="70"/>
            <Setter Property="FontAttributes" Value="Bold"/>
        </Style>            
    </ResourceDictionary>
</Application.Resources></Application>

Then in the code behind

Button newButton1 = new Button
{
    Text = "Hello",
    WidthRequest = (double)15.0,
    Style = (Style)Application.Current.Resources["BlueButton"]
};
3not3
  • 516
  • 1
  • 7
  • 18
13

You can set, Something like this,

  TextBlock myTextBlock= new TextBlock ()
    {
        FontFamily = new FontFamily("Segoe UI Light");
        Style = Resources["TextBlockStyle"] as Style,
    };
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
  • I see this is old but does anyone know how to do this exact same thing with FrameworkElementFactory(typeof(TextBlock)) ? – grinder22 Sep 07 '16 at 15:02
3

You can use this:

Style textBlockStyle;
try
{
    textBlockStyle = FindResource("TextBlockStyle") as Style;
}
catch(Exception ex)
{
    // exception handling
}

if(textBlockStyle != null)
{
    myTextBlock.Style = textBlockStyle;
}

or TryFindResource approach:

myTextBlock.Style = (Style)TryFindResource("TextBlockStyle");
Hamlet Hakobyan
  • 32,965
  • 6
  • 52
  • 68
0
 private void initializedynamicButton()
    {
        pnlContent.Children.Clear();
        for (int i = 0; i < 25; i++)
        {
            Button btn = new Button();
           btn.Style = (Style)this.FindResource("GelButton");
            btn.Content = new
            {
                Text = "ButtonName_"+i.ToString(),
                ImagePath = "",
                Margin = new Thickness(0, 0, 0, 10)

            };
            btn.Tag = new { GroupID = i };
            btn.Click += new RoutedEventHandler(btn_Click);
            pnlContent.Children.Add(btn);
        }


    }
Anandu
  • 1
  • 2
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 24 '23 at 14:43