我剛開始使用WPF,特別是驗證和數據綁定。基本的WPF驗證和DataBinding
這是我的XAML代碼
<Window x:Class="simpledatagrid.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="IDDATA" Height="350" Width="525">
<Grid >
<DataGrid SelectionChanged="Iddetails" Name="dgsample" BorderBrush="Black" BorderThickness="2" AutoGenerateColumns="True" CanUserAddRows="True" CanUserDeleteRows="True" CanUserSortColumns="False" Margin="200,10,10,75"></DataGrid>
<Label Content="ID :" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="26" Width="27"/>
<Label Content="Name :" HorizontalAlignment="Left" Margin="10,60,0,0" VerticalAlignment="Top" Height="26" Width="48"/>
<Label Content="Salary :" HorizontalAlignment="Left" Margin="10,110,0,0" VerticalAlignment="Top" Height="26" Width="47"/>
<TextBox Name="tb1" HorizontalAlignment="Left" Height="20" Margin="60,10,0,0" TextWrapping="NoWrap" Text="" VerticalAlignment="Top" Width="100" />
<TextBox Name="tb2" HorizontalAlignment="Left" Height="20" Margin="60,60,0,0" TextWrapping="NoWrap" Text="" VerticalAlignment="Top" Width="100"/>
<TextBox Name="tb3" HorizontalAlignment="Left" Height="20" Margin="60,110,0,0" TextWrapping="NoWrap" Text="" VerticalAlignment="Top" Width="100"/>
<Button Content="Get" HorizontalAlignment="Left" Margin="10,190,0,0" VerticalAlignment="Top" Width="75" Click="Get_Click" />
<Button Content="Add" HorizontalAlignment="Left" Margin="10,230,0,0" VerticalAlignment="Top" Width="75" Click="Add_Click" />
<Button Content="Delete" HorizontalAlignment="Left" Margin="10,270,0,0" VerticalAlignment="Top" Width="75" Click="Delete_Click" />
</Grid>
這是我.CS碼
public partial class MainWindow : Window
{
ObservableCollection<User> Users = new ObservableCollection<User>();
public MainWindow()
{
InitializeComponent();
Users.Add(new User() { Id = 101, Name = "leon", Salary = 10 });
Users.Add(new User() { Id = 102, Name = "allen", Salary = 20 });
Users.Add(new User() { Id = 103, Name = "neon", Salary = 30 });
Users.Add(new User() { Id = 104, Name = "xeln", Salary = 40 });
Users.Add(new User() { Id = 105, Name = "kalen", Salary = 50 });
Users.Add(new User() { Id = 106, Name = "velen", Salary = 60 });
dgsample.ItemsSource = Users;
}
private void Iddetails(object sender, SelectionChangedEventArgs args)
{
int index = dgsample.SelectedIndex;
tb1.Text = Users[index].Id.ToString();
tb2.Text = Users[index].Name;
tb3.Text = Users[index].Salary.ToString();
}
private void Get_Click(object sender, RoutedEventArgs e)
{
int index;
if (int.TryParse(this.tb1.Text, out index))
{
User currentUser = Users.FirstOrDefault(Select => Select.Id == int.Parse(tb1.Text));
if (currentUser != null)
{
this.tb2.Text = currentUser.Name;
this.tb3.Text = currentUser.Salary.ToString();
}
else
MessageBox.Show("User with the provided ID does not Exist", "Error");
}
else
MessageBox.Show("ID entered is not valid number", "Error");
}
private void Add_Click(object sender, RoutedEventArgs e)
{
if (!tb1.Text.Equals(""))
{
var adduser = Users.Where(User => User.Id == int.Parse(tb1.Text));
if (!adduser.Any())
{
Users.Add(new User() { Id = int.Parse(tb1.Text), Name = tb2.Text, Salary = int.Parse(tb3.Text) });
}
else
MessageBox.Show("Someone already has that ID.");
}
}
private void Delete_Click(object sender, RoutedEventArgs e)
{
int index;
if (int.TryParse(this.tb1.Text, out index))
{
User currentUser = Users.FirstOrDefault(Select => Select.Id == int.Parse(tb1.Text));
if (currentUser != null)
{
Users.Remove(currentUser);
}
else
MessageBox.Show("User with the provided ID does not Exist", "Error");
}
else
MessageBox.Show("ID entered is not valid number", "Error");
}
}
此代碼工作,但我使用數據綁定和驗證的概念,需要同樣的事情對於TextBoxes,請幫我使用所需的代碼
你」重新使用像你一樣的WPF會做Windows窗體。我強烈建議你閱讀關於MVVM。此模式也使驗證更容易。 –
一般而言,您可以通過例如'Content = {Binding Path = content ElementName = name}將控件的屬性綁定到其他控件的code-behind/property中的屬性' – Tafari