0

I have a Style in WPF for a custom control. The style looks like this:

<Style x:Key="MyStyle" TargetType="Button">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid x:Name="LayoutRoot">

                        <Rectangle x:Name="background" Width="450" Height="450" RadiusX="3" RadiusY="3" Fill="White" />
                        <TextBlock Name="MyText" Foreground="Black" HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="5" FontSize="52" Text="Text"/>

                        <ContentPresenter x:Name="content" />
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter TargetName="background" Property="Fill" Value="Red" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
</Style>

I want to remove the "MyText" TextBlock from the XAML code and create it programmatically, then add it to the Style.

//Create the TextBlock using a method I wrote myself
TextBlock MyTest = CreateTextBlock("Text", Brushes.Black, HorizontalAlignment.Left, VerticalAlignment.Bottom, 52);

//Now get the style I want to change
Style CurrentStyle = (Style)FindResource("MyStyle");

//What next?

How do I now add the TextBlock programmatically so that the Style matches what it was before?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ben
  • 31
  • 7
  • you can make it a part of Content and it will be displayed in ContentPresenter – ASh Oct 25 '19 at 09:24
  • @ASh Can you provide the code for how to do this? I'm not very savvy with WPF. – Ben Oct 25 '19 at 09:26
  • If that's just meant to set the Text, why don't you bind Text to another property of your control? As usual, with a TemplateBinding. – Clemens Oct 25 '19 at 09:28
  • @Clemens The method examines the text string that is passed into it so that some parts of the text (not all the text) can be made bold, underlined, etc. It is a regular string in this instance, but this will not always be the case. As far as I know, this needs to be done programmatically. – Ben Oct 25 '19 at 09:31
  • You mean you are creating elements for the TextBlock's `Inlines` collection? – Clemens Oct 25 '19 at 09:33
  • @Clemens Yes, that's right. – Ben Oct 25 '19 at 09:40
  • You may still do that by a Binding, with the help of an attached property that manipulates the Inlines collection. See e.g. here: https://stackoverflow.com/q/5565885/1136211 – Clemens Oct 25 '19 at 09:41
  • @Clemens Is it not possible to do this programmatically then? – Ben Oct 25 '19 at 09:56
  • Somehow, maybe. It is however definitely not the recommended way. Assuming that you already have an InlineCollection property in your control, it would be totally simple to declare an attached property that you can apply to the TextBlock. That said, where does you custom control come into play here? What you are showing is a Style for a Button. – Clemens Oct 25 '19 at 10:00
  • Or just add a TextChanged event handler to your TextBlock and manipulate the inlines in there. – GazTheDestroyer Oct 25 '19 at 10:05
  • @Gaz It's not a TextBox.... – Clemens Oct 25 '19 at 10:17

0 Answers0