PDA

View Full Version : سوال: Data Binding



hadi1370
دوشنبه 19 اردیبهشت 1390, 15:35 عصر
سلام
داخل آموزشی که آقای وحید نصیری برای data binding گذاشتن مشکل داشتم.
داخل این برنامه دو تا پوشه داریم یکی به اسم Model و یکی دیگه به اسم ViewModel. داخل Model یه کلاس دارم به اسم MyCustomer :

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace DataBinding.Model
{
public class MyCustomer
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public string Country { get; set; }
}
}


و کلاس CustomerViewModel رو در داخل پوشه ی ViewModel دارم:


using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DataBinding.Model;

namespace DataBinding.ViewModel
{
public class CustomerViewModel
{
public MyCustomer Customer { get; set; }

public CustomerViewModel()
{
this.Customer = new MyCustomer
{
FirstName = "Hadi",
LastName = "Sinaee",
Age = 21,
Country = "Iran"
};
}
}
}


حالا داخل MainPage.xaml کد های زیر رو دارم( چند تا label و چند تا textBox دارم ):

<UserControl x:Class="DataBinding.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" Loaded="UserControl_Loaded"
xmlns:src="clr-namespace:DataBinding.Model"
d:DesignHeight="300" d:DesignWidth="400" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
<Grid x:Name="LayoutRoot" Background="White">
<sdk:Label Height="22" HorizontalAlignment="Left" Margin="12,26,0,0" Name="label1" VerticalAlignment="Top" Width="71" Content="First Name"/>
<sdk:Label Height="22" HorizontalAlignment="Left" Margin="12,58,0,0" Name="label2" VerticalAlignment="Top" Width="71" Content="Last Name"/>
<sdk:Label Height="22" HorizontalAlignment="Left" Margin="12,94,0,0" Name="label3" VerticalAlignment="Top" Width="71" Content="Age"/>
<sdk:Label Height="22" HorizontalAlignment="Left" Margin="12,131,0,0" Name="label4" VerticalAlignment="Top" Width="71" Content="Country"/>
<TextBox Height="23" HorizontalAlignment="Left" Margin="107,22,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" Text="{Binding MyCustomer.FirstName}"/>
<TextBox Name="textBox2" Margin="108,54,173,222" HorizontalAlignment="Center" VerticalAlignment="Center" Width="119" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="107,90,0,0" Name="textBox3" VerticalAlignment="Top" Width="120" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="107,127,0,0" Name="textBox4" VerticalAlignment="Top" Width="120" />

</Grid>
</UserControl>




داخل MainPage.xaml.cs هم دارم:


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 DataBinding.ViewModel;
namespace DataBinding
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();

this.DataContext = new CustomerViewModel();
}
}
}


ولی انقیاد انجام نمیشه ؟ کسی می تونه کمک کنه؟

chaalesh
دوشنبه 19 اردیبهشت 1390, 21:21 عصر
چرا از خود آقای نصیری نمی پرسید ؟