从 ControlTemplate 按名称获取代码中的控件

发布于 2024-08-31 20:34:32 字数 977 浏览 2 评论 0原文

我的 WPF 应用程序中有下一个控件模板。

    <Style TargetType="Label" x:Key="LabelStyle">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Label">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="40"/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>
                        <TextBox x:Name="MyTextBlock" Text="{TemplateBinding Content}"  Height="20" HorizontalAlignment="Left"  VerticalAlignment="Top" />
                        <Label Content="{TemplateBinding Content}" Grid.Column="1" Grid.Row="1"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

文本框“MyTextBlock”在窗口的 C# 代码中不可见。如何在代码中访问此文本块

I have next control template in my WPF app.

    <Style TargetType="Label" x:Key="LabelStyle">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Label">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="40"/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>
                        <TextBox x:Name="MyTextBlock" Text="{TemplateBinding Content}"  Height="20" HorizontalAlignment="Left"  VerticalAlignment="Top" />
                        <Label Content="{TemplateBinding Content}" Grid.Column="1" Grid.Row="1"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

TextBox "MyTextBlock" is invisible in C# code of window. How can I access to this textblock in code

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一指流沙 2024-09-07 20:34:32

尝试将您的属性直接绑定到文本框的可见性,

<TextBox Visibility="{Binding IsFieldCodesEnabled, Converter={StaticResource BoolToVis}}" />

其中 BoolToVis 定义为:

<Resouces> 
    <loc:BooleanToVisibilityConverter k:key="BoolToVis"/> 
</Resources>

Try binding your property to the textbox's visibility directly

<TextBox Visibility="{Binding IsFieldCodesEnabled, Converter={StaticResource BoolToVis}}" />

where BoolToVis is defined as:

<Resouces> 
    <loc:BooleanToVisibilityConverter k:key="BoolToVis"/> 
</Resources>
阳光下的泡沫是彩色的 2024-09-07 20:34:32

您可以在后面的代码中执行与 XAML 页面类似的操作(除了您需要在 OnApplyTemplate 覆盖中执行此操作):

public override void OnApplyTemplate() {
    base.OnApplyTemplate();

    var MyTextBlock = this.GetTemplateChild("MyTextBlock")
}

编辑 只是注意到 MyTextblock 实际上是一个 TextBox ,因此将 TextBox 转换为 TextBlock 将导致异常。尝试更新的代码。

You can do a similar thing that XAML pages do in the code behind (except for you need to do it in OnApplyTemplate override):

public override void OnApplyTemplate() {
    base.OnApplyTemplate();

    var MyTextBlock = this.GetTemplateChild("MyTextBlock")
}

EDIT Just noticed that MyTextblock is actually a TextBox, so casting a TextBox to TextBlock will cause an exception. Try the updated code.

岁月苍老的讽刺 2024-09-07 20:34:32

我为我的情况找到了一些解决方案。我只是在我的 ControlTemplate 中使用 TextBox 的已加载事件,

    private void MyTextBlock_Loaded(object sender, RoutedEventArgs e)
    {
        TextBox txt = sender as TextBox;
        if (txt!=null)
         {
            Messagebox.Show("It works");
         }

    }

但这不是那么漂亮的解决方案。

I found some solution for my situation. I just use loaded event of TextBox in my ControlTemplate

    private void MyTextBlock_Loaded(object sender, RoutedEventArgs e)
    {
        TextBox txt = sender as TextBox;
        if (txt!=null)
         {
            Messagebox.Show("It works");
         }

    }

But it is not so beautiful solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文