TextBlock:
前台代码
<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="VerticalAlignment" 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文件数据查询界面" 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="请输入员工姓名:"></TextBlock>
<TextBox x:Name="txtNameSearch" Width="100">
<TextBox.Text>高</TextBox.Text></TextBox>
<Button x:Name="btnSearch" Content="查询" 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="姓名:" TextAlignment="Right" Style="{StaticResource lbl}"></TextBlock>
<TextBox x:Name="txtName" Width="100"></TextBox>
</StackPanel>
<StackPanel Orientation="Horizontal" >
<TextBlock Text="性别:" TextAlignment="Right" Style="{StaticResource lbl}"></TextBlock>
<TextBox x:Name="txtSex" IsEnabled="True" IsReadOnly="False" Width="100"></TextBox>
</StackPanel>
</StackPanel>
</Border>
</StackPanel>
</Grid>
后台代码:
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;//输入的查询条件
private void btnSearch_Click(object sender, RoutedEventArgs e)
{
if (string.IsNullOrEmpty(this.txtNameSearch.Text.Trim()))
{
MessageBox.Show("请输入查询条件!");
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("抱歉,找不到!");
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; }
}
}