wpf模板问题
怎么向模板里面的控件加内容啊,比如我做了个textbox控件的样式,里面有个controltemplate,里面有个grid,grid有2个元素:button和textb...
怎么向模板里面的控件加内容啊,比如我做了个textbox控件的样式,里面有个controltemplate,里面有个grid,grid有2个元素:button和textbox1。我在主界面调用了这个模板的样式后,怎么读取textbox1的text?
展开
1个回答
展开全部
模板内的属性可以绑定到控件本身的属性,关键字是TemplateBinding
<Window x:Class="WpfApplication5.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Grid>
<TextBox Name="textBox1" HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBox.Template>
<ControlTemplate TargetType="TextBox">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<TextBox Text="{TemplateBinding Text}" Background="LightBlue" MinWidth="50"/>
<Button Grid.Column="1">button1</Button>
</Grid>
</ControlTemplate>
</TextBox.Template>
</TextBox>
</Grid>
</Window>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.textBox1.Text = "Hello World!";
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询