wpf 创建搜索框控件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35988422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create Search Box Control
提问by Andre Roque
I'm trying to create a search text box where I have a Text Box area plus the button. So far I achieve my expected layout:
我正在尝试创建一个搜索文本框,其中有一个文本框区域和按钮。到目前为止,我实现了预期的布局:
<Style TargetType="{x:Type local:SearchTextBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:SearchTextBox}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<StackPanel Orientation="Horizontal"
Width="200">
<TextBox Width="150" TextWrapping="Wrap" AcceptsReturn="True"/>
<Button Width="50" Content="Browse"></Button>
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Then I created the Control:
然后我创建了控件:
static SearchTextBox()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(SearchTextBox), new FrameworkPropertyMetadata(typeof(SearchTextBox)));
}
TextBox txtFileName = null;
public TextBox TxtFileName
{
get { return txtFileName; }
set { txtFileName = value; }
}
Button btnBrowse = null;
public Button BtnBrowse
{
get { return btnBrowse; }
set { btnBrowse = value; }
}
Now I would like to access this button and text box so I can define their name, content, etc individually.
现在我想访问这个按钮和文本框,以便我可以单独定义它们的名称、内容等。
How can I do it?
我该怎么做?
回答by Anees Deen
In your case, you can simply use UserControlsto create your custom control.
在您的情况下,您可以简单地使用UserControls来创建您的自定义控件。
Here is the link to create a sample usercontrol. Extend the control based on your requirement
这是创建示例用户控件的链接。根据您的要求扩展控制
http://www.c-sharpcorner.com/UploadFile/mahesh/user-control-in-wpf/
http://www.c-sharpcorner.com/UploadFile/mahesh/user-control-in-wpf/
回答by Brian
Overide this method in your class. public virtual void OnApplyTemplate(). Then create and instantiate the controls that you need. something along these lines:
在您的课程中覆盖此方法。公共虚拟无效 OnApplyTemplate()。然后创建并实例化您需要的控件。沿着这些路线的东西:
GetTemplatechild will allow you to get any dependency objects that have been defined. Do give them a name (x:Name="foo").
GetTemplatechild 将允许您获取任何已定义的依赖对象。给他们一个名字 (x:Name="foo")。
public override void OnApplyTemplate()
{
DependencyObject ButtonControlInTemplate = GetTemplateChild("searchbutton");// set the name as the x:Name for the controls in your xaml.
Button SearchButton = (Button)ButtonControlInTemplate;
DependencyObject TextBoxInTemplate = GetTemplateChild("searchinputfield"); // set the name as the x:Name for the controls in your xaml.
TextBox InputTextBox = (TextBox)TextBoxInTemplate;
base.OnApplyTemplate();
}
NB. its important to check for null, sometimes the template isn't applied properly.
注意。检查空值很重要,有时模板应用不正确。

