Acces Textblock Text inside a ButtonStyle from codebehind

▼魔方 西西 提交于 2019-12-12 02:48:50

问题


How to access the tbRegistrationBtn.text property from code behind from a custom made style?

My button is being created dynamically from codebehind and gets added to the parent control (stackpanel): The button gets created when i press a other button on my screen.

Codebehind:

                Button newBtn = new Button();
                newBtn.Width = 160;
                newBtn.Height = 46;
                newBtn.Style = this.FindResource("ButtonStyleRegistration") as Style;
                spHorizontal.Children.Add(newBtn);

Xaml:

        <Style x:Key="ButtonStyleRegistration" TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid x:Name="registrationButton">
                        <Rectangle Fill="#FF89959A" Height="Auto" RadiusY="15" RadiusX="15" Stroke="White" Width="Auto"/>
                        <TextBlock x:Name="tbRegistrationBtn" TextWrapping="Wrap" Text="" HorizontalAlignment="Center" Margin="7.5,14.973,0,16.84" d:LayoutOverrides="Height"/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsFocused" Value="True"/>
                        <Trigger Property="IsDefaulted" Value="True"/>
                        <Trigger Property="IsMouseOver" Value="True"/>
                        <Trigger Property="IsPressed" Value="True"/>
                        <Trigger Property="IsEnabled" Value="False"/>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="FontSize" Value="10.667"/>
    </Style> 

Any attempt to retrieve the textblock results in a null error. Attempt:

            Style style = this.FindResource("ButtonStyleRegistration") as Style;
            newBtn.Style = style;
            TextBlock tb = (TextBlock)style.Resources.FindName("tbRegistrationBtn");
            tb.Text = "test";

Best regards.


回答1:


You can use VisualTreeHelper to navigate though visual tree of your button. Use this helper function:

public static T FindVisualChild<T>(DependencyObject obj, string name)
    where T : DependencyObject
{
    var count = VisualTreeHelper.GetChildrenCount(obj);
    for(int i = 0; i < count; i++)
    {
        var child = VisualTreeHelper.GetChild(obj, i);

        if(child != null)
        {
            var res = child as T;
            if(res != null && (string)res.GetValue(FrameworkElement.NameProperty) == name)
            {
                return res;
            }
            res = FindVisualChild<T>(child, name);
            if(res != null) return res;
        }
    }

    return null;
}

Also you need to force your button to build it's visual tree based on template (because it is delayed by default):

newBtn.ApplyTemplate();

And finally set your TextBlock text:

var tb = FindVisualChild<TextBlock>(newBtn, "tbRegistrationBtn");
tb.Text = "Registration";



回答2:


The answer of max is definitly right for your situation. However, why not do this:

<TextBlock TextWrapping="Wrap" Text="{TemplateBinding Content}"
           HorizontalAlignment="Center" Margin="7.5,14.973,0,16.84"/>

than you can set newBtn.Content = "test" in code-behind.



来源:https://stackoverflow.com/questions/9715918/acces-textblock-text-inside-a-buttonstyle-from-codebehind

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!