انقیاد دادهها در WPF (بخش اول)
نویسنده: مهدی ملائیان
تاریخ: ۱۳۹۴/۰۹/۰۴ ۱۶:۱۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
public class Employee
{
public string Name { get; set; }
public string Title { get; set; }
public static Employee GetEmployee()
{
var emp = new Employee
{
Name = "Mani",
Title = "CEO"
};
return emp;
}
} <Grid>
<StackPanel Name="Display">
<StackPanel Orientation="Horizontal">
<TextBlock>First Name :</TextBlock>
<TextBlock Margin="5,0,0,0" Text="{Binding Name}"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock>Title :</TextBlock>
<TextBlock Margin="5,0,0,0" Text="{Binding Title}"></TextBlock>
</StackPanel>
</StackPanel>
</Grid> public MainWindow()
{
InitializeComponent();
DataContext = Employee.GetEmployee();
} public class Employee :INotifyPropertyChanged
{
public string Name { get; set; }
public string Title { get; set; }
public static Employee GetEmployee()
{
var emp = new Employee
{
Name = "Mani",
Title = "CEO"
};
return emp;
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged
([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
} private string _name;
private string _title;
public string Name
{
get { return _name; }
set
{
_name = value;
OnPropertyChanged();
}
}
public string Title
{
get { return _title; }
set
{
_title = value;
OnPropertyChanged();
}
} private Employee emp;
public MainWindow()
{
InitializeComponent();
emp = new Employee()
{
Name = "Mani",
Title = "CEO"
};
DataContext = emp;
} <StackPanel Orientation="Horizontal">
<Button Click="btnClick" Width="70" Height="30" Content="Change"/>
</StackPanel> private void btnClick(object sender, RoutedEventArgs e)
{
emp.Name = "Amir";
emp.Title = "Manager";
} <Grid>
<StackPanel Name="Display" >
<StackPanel Orientation="Horizontal">
<TextBlock Margin="5,0,0,0">Name :</TextBlock>
<TextBox Margin="5,0,0,0" Text="{Binding Name, Mode=TwoWay}"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Margin="5,0,0,0">Title :</TextBlock>
<TextBox Margin="5,0,0,0" Text="{Binding Title,Mode=TwoWay}"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Margin="5,0,0,0">Name :</TextBlock>
<TextBlock Margin="5,0,0,0" Text="{Binding Name}"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Margin="5,0,0,0">Title :</TextBlock>
<TextBlock Margin="5,0,0,0" Text="{Binding Title}"/>
</StackPanel>
</StackPanel>
</Grid>