코드가 있습니다 XAML
.
<Button Grid.Column="0" Grid.Row="1" x:Name="btnSete" Click="btn_Click">
<ContentControl.Content>
<Viewbox Margin="3">
<TextBlock Text="7"/>
</Viewbox>
</ContentControl.Content>
</Button>
그리고 뒤에 코드 :
private void btn_Click(object sender, RoutedEventArgs e)
{
Button btn = (Button)e.Source;
txbDisplay.Text = btn.Content.ToString();
}
어떻게 값합니까 <TextBlock Text="7"/>
에서를 btn_Click
?
당신의 XAML을 기반 btn.Content
의 인스턴스해야한다 Viewbox
. 그 다음 Child
의 속성은 Viewbox
당신을해야한다 TextBlock
.
따라서이 작업을 수행 할 수 있어야합니다 (버튼 클릭 이벤트 핸들러 내에서).
var viewBox = (Viewbox)btn.Content;
var textBlock = (TextBlock)viewBox.Child;
var text = textBlock.Text; // This is the value you were looking for
txbDisplay.Text = text;
또한 유용한 참고 사항과 마찬가지로 <ContentControl.Content> ... </ContentControl.Content>
XAML 의 일부는 불필요합니다. 해당 블록은 다음과 같이 작성할 수 있습니다.
<Button Grid.Column="0" Grid.Row="1" x:Name="btnSete" Click="btn_Click">
<Viewbox Margin="3">
<TextBlock Text="7"/>
</Viewbox>
</Button>
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다