在运行时/生成时访问controlTemplate元素 [英] Access a controlTemplate element at runtime/generation
问题描述
我有以下ControlTemplate:
Hi, I have the following ControlTemplate:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ControlTemplate x:Key="ComponentTemplate" x:Name="componentTemplate">
<Border BorderBrush="Black" BorderThickness="2,2,2,2" Width="104" Height="44" CornerRadius="8,8,8,8">
<Canvas>
<Rectangle Name="tplRect" Width="100" Height="40" Fill="LightCyan" RadiusX="8" RadiusY="8" />
<TextBox Name="tplTextBox" Text="Component" HorizontalAlignment="Center" Margin="17,10"/>
</Canvas>
</Border>
</ControlTemplate>
</ResourceDictionary>
我希望能够使用C#代码访问文本框,我该怎么做?
I want to be able to access the textbox in C# code, how would I go about doing that?
推荐答案
用
TextBox
?您愿意访问它的Text值非常简单,只需要将其绑定到类的属性上即可.
That depends, what are you willing to do with the
TextBox
? With you are willing to access the Text value of it its fairly simple, you only need to bind it to a property from a class.
如果您愿意访问它的其他属性,我将创建一个标记为TextBox
的附加属性,以便在其他地方使用,因此在TextBox
中,您将添加AP,在PropertyChangedCallback
中,添加自定义保持引用的逻辑.
If you are willing to access other properties of it, I would create an attached property that mark the TextBox
for use elsewhere, so in the TextBox
you would add the AP and in the PropertyChangedCallback
add a custom logic to keep the reference to it.
希望这会有所帮助,如果没有告诉我,我会尽力帮助您.
Hope this helps, if doesn''t tell me and I will try to help you out.
一切顺利
劳尔
这篇关于在运行时/生成时访问controlTemplate元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!