我有一个带有自定义模板的按钮。您是否知道如何通过双击重命名按钮中的内容。 这就是我所拥有的,但是从代码中我无法访问b1.textBox来将可见性设置为可见。
<Window.Resources>
<Style TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid>
<Ellipse x:Name="outerCircle" Fill="Red" Height="50" Width="50"/>
<Ellipse x:Name="innerCircle" Fill="White" RenderTransformOrigin=".5,.5">
<Ellipse.RenderTransform>
<ScaleTransform ScaleX=".9" ScaleY=".9"/>
</Ellipse.RenderTransform>
</Ellipse>
<Grid>
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
<TextBox x:Name="textBox" Visibility="Collapsed"/>
</Grid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
void button1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
//MessageBox.Show(e.Source.ToString());
Button b1 = (Button)e.Source;
// b1.textBox doesn't get displayed here.
}
答案 0 :(得分:1)
怎么样
b1.Template.FindName("textBox",b1) as TextBox
答案 1 :(得分:0)
为什么不将它添加到样式中?
<EventSetter Event="MouseDoubleClick" Handler="HandleDoubleClick" />
然后在代码背后:
protected void HandleDoubleClick(object sender, MouseButtonEventArgs e)
{
Button b1 = (Button)sender;
}