TextBlock:
Front-end code
<UserControl. Resources>
<Style TargetType = "TextBox">
<Setter Property = "Width" Value = "120"> </Setter>
</Style>
<Style TargetType = "TextBlock" x: Key = "lbl">
<Setter Property = "Width" Value = "100"> </Setter>
<Setter Property = "TextAlignment" Value = "Right"> </Setter>
<Setter Property = "verticalignment" Value = "Center"> </Setter>
</Style>
</UserControl. Resources>
<Grid x: Name = "LayoutRoot" Background = "White" ShowGridLines = "True">
<Grid. RowDefinitions>
<RowDefinition Height = "50"> </RowDefinition>
<RowDefinition> </RowDefinition>
</Grid. RowDefinitions>
<Grid. ColumnDefinitions>
<ColumnDefinition> </ColumnDefinition>
<ColumnDefinition Width = "60"> </ColumnDefinition>
</Grid. ColumnDefinitions>
<TextBlock x: Name = "title" Text = "XML file data query interface" FontWeight = "Bold" Grid. row = "0" FontSize = "16" Grid. columnSpan = "3" Grid. column = "0" HorizontalAlignment = "Center"> </TextBlock>
<StackPanel FlowDirection = "LeftToRight" Orientation = "Horizontal" Grid. row = "0" Height = "28" Grid. columnSpan = "2" HorizontalAlignment = "Center" VerticalAlignment = "Bottom">
<TextBlock Text = "Enter employee name:"> </TextBlock>
<TextBox x: Name = "txtNameSearch" Width = "100">
<TextBox. Text> height </TextBox. Text> </TextBox>
<Button x: Name = "btnSearch" Content = "query" Padding = "20, 5" Click = "btnSearch_Click"> </Button>
</StackPanel>
<StackPanel Grid. Row = "1" Grid. ColumnSpan = "2" x: Name = "SPContent">
<Border Width = "auto" Height = "auto" BorderThickness = "1" CornerRadius = "5" BorderBrush = "# 39c">
<StackPanel>
<StackPanel Orientation = "Horizontal">
<TextBlock Text = "name:" TextAlignment = "Right" Style = "{StaticResource lbl}"> </TextBlock>
<TextBox x: Name = "txtName" Width = "100"> </TextBox>
</StackPanel>
<StackPanel Orientation = "Horizontal">
<TextBlock Text = "Gender:" TextAlignment = "Right" Style = "{StaticResource lbl}"> </TextBlock>
<TextBox x: Name = "txtSex" IsEnabled = "True" IsReadOnly = "False" Width = "100"> </TextBox>
</StackPanel>
</StackPanel>
</Border>
</StackPanel>
</Grid>
Background code:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Net;
Using System. Windows;
Using System. Windows. Controls;
Using System. Windows. Documents;
Using System. Windows. Input;
Using System. Windows. Media;
Using System. Windows. Media. Animation;
Using System. Windows. Shapes;
Using System. Xml. Linq;
Namespace TextBoxDemo
{
Public partial class MainPage: UserControl
{
Public MainPage ()
{
InitializeComponent ();
}
String valueToSearch; // input query Conditions
Private void btnSearch_Click (object sender, RoutedEventArgs e)
{
If (string.IsNullOrEmpty(this.txt NameSearch. Text. Trim ()))
{
MessageBox. Show ("Enter the query condition! ");
Return;
}
Else {
ValueToSearch = txtNameSearch. Text. Trim ();
WebClient client = new WebClient ();
Client. DownloadStringCompleted + = new DownloadStringCompletedEventHandler (client_DownloadStringCompleted );
Client. DownloadStringAsync (new Uri ("../XML/Employees. xml", UriKind. Relative ));
}
}
Private void client_DownloadStringCompleted (object sender, DownloadStringCompletedEventArgs e ){
XDocument document = XDocument. Parse (e. Result );
XElement el = XElement. Load (document. CreateReader ());
Var items = (
From item in el. Elements ("employess"). Elements ("item ")
Where item. Element ("name"). Value. StartsWith (valueToSearch)
Select new Employee
{
EName = (string) item. Element ("name ")),
ESex = (string) item. Element ("gender ")}
). Take (1 );
If (items. Count () = 0)
{
MessageBox. Show ("Sorry, it cannot be found! ");
SPContent. DataContext = null;
}
Else {
Employee p = items. ElementAt <Employee> (0 );
System. Windows. Data. Binding binding = new System. Windows. Data. Binding ("");
Binding. Mode = System. Windows. Data. BindingMode. OneWay;
Binding. Source = p. EName;
TxtName. SetBinding (TextBox. TextProperty, binding );
}
}
}
Class Employee {
Public string EName {get; set ;}
Public string ESex {get; set ;}
}
}