Template 내부의 요소를 FindName 으로 찾기

wavecat·2022년 9월 14일
0

WPF

목록 보기
2/2
<Popup x:Name="MD_popup"  PlacementTarget="{Binding ElementName=PDT_MainName}" 
        Placement="Bottom"  Width="350" Height="130" >
    <Border BorderBrush="Black" BorderThickness="2" Background="AliceBlue">
        <TextBox x:Name="MD_PDTName" VerticalAlignment="Center" Margin="15,0,15,0">
            <TextBox.Style>
                <Style TargetType="TextBox">
                    <Setter Property="Width" Value="{Binding RelativeSource={RelativeSource self}, Path=Width}"/>
                    <Setter Property="Height" Value="{Binding RelativeSource={RelativeSource self}, Path=Height}"/>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type TextBox}">
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="70"/>
                                        <RowDefinition Height="*"/>
                                    </Grid.RowDefinitions>
                                    <TextBox Grid.Row="0" x:Name="Target_Name" VerticalAlignment="Center" 
                                                Height="50" Width="300" Text="{Binding NName}" FontSize="25" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
                                    <Grid Grid.Row="1">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="0.5*"/>
                                            <ColumnDefinition Width="0.5*"/>
                                        </Grid.ColumnDefinitions>
                                        <Button Grid.Column="0" Content="적용" Click="Modify_Click" Height="30" Width="50" />
                                        <Button Grid.Column="1" Content="닫기" Click="Popup_Close_Click" Height="30" Width="50" />
                                    </Grid>
                                </Grid>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </TextBox.Style>
        </TextBox>
    </Border>
</Popup>

여기서 Template 내부의 요소인 TextBox 의 Name 인 Target_Name 을 코드상으로 선택하려는데
코드상에서 Target_Name 은 표시되지 않는다.
Target_Name 은 Template 내부의 요소이기 때문이다.
이 경우에는 FindName 메서드를 호출하면 된다.

TextBox nnTB = (TextBox)MD_PDTName.Template.FindName("Target_Name", MD_PDTName);

popup의 이름을 먼저 호출하지 말고 TextBox 의 Name 인 MD_PDTName 을 먼저 호출하고
Template 를 호출한뒤 Template 의 FindName 메서드를 호출한다.
그 다음 "Target_Name" 이라는 Name 을 가진 요소를 찾게하고 이것을 nnTB 라는 객체로 선언

profile
아무거나 공부한거 올리기

0개의 댓글