Step 1. XAML,給予要存取元件名稱
1: <Style TargetType="local:WaitReadyButton">
2: <Setter Property="Template">
3: <Setter.Value>
4: <ControlTemplate TargetType="local:WaitReadyButton">
5: <Grid>
6: <TextBlock x:Name="txtSymbol" Text=""/>
7: </Grid>
8: </ControlTemplate>
9: </Setter.Value>
10: </Setter>
11: </Style>
Step 2. C#,宣告存取元件
1: public sealed class WaitReadyButton : Control
2: {
3: private TextBlock _txtSymbol;
Step 3. C#,在OnApplyTemplate()中取得元件
1: protected override void OnApplyTemplate()
2: {
3: base.OnApplyTemplate();
4:
5: _txtSymbol = GetTemplateChild(@"txtSymbol") as TextBlock;
6: }
成功取得元件後即可在程式碼中使用該元件。
Reference
沒有留言:
張貼留言