从代码背后访问ButtonStyle内的Textblock文本 [英] Acces Textblock Text inside a ButtonStyle from codebehind

查看:105
本文介绍了从代码背后访问ButtonStyle内的Textblock文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过自定义样式从后面的代码访问 tbRegistrationBtn.text 属性?

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.

隐藏代码:

                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> 

任何尝试检索文本块的操作都会导致null错误. 尝试:

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";

最诚挚的问候.

推荐答案

您可以使用VisualTreeHelper浏览按钮的可视树.使用此辅助功能:

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();

最后设置您的TextBlock文本:

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

这篇关于从代码背后访问ButtonStyle内的Textblock文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆