2013-07-22 4 views
0

La liaison d'une zone de texte à une autre n'est pas correctement effectuée. Voici l'utilisation du code ci-dessous.Les données de la zone de texte d'une fenêtre ne s'affichent pas dans la zone de texte d'une autre fenêtre

EDIT:

mainWindow xaml:

<Grid> 
    <TextBox Name="txtBox1" AcceptsReturn="True" Margin="0,0,203,148" VerticalScrollBarVisibility="Visible" LostFocus="txtBox1_LostFocus" TextChanged="txtBox1_TextChanged"> 

    </TextBox> 
    <Button Content="ButtonToDisplay" Height="46" HorizontalAlignment="Left" Margin="362,71,0,0" Name="button1" VerticalAlignment="Top" Width="98" Click="button1_Click" /> 
</Grid> 

CheckAddressWindow xaml:

 <Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="195"/> 
     <RowDefinition Height="28"/> 
     <RowDefinition Height="50"/> 
    </Grid.RowDefinitions> 
    <GroupBox Name="grpFullName" Header="Name Details" BorderBrush="Black" BorderThickness="1" FontWeight="Bold" > 
     <Grid ShowGridLines="False"> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="15"/> 
       <RowDefinition Height="50"/> 
       <RowDefinition Height="25"/> 
       <RowDefinition Height="25"/> 
       <RowDefinition Height="25"/> 
       <RowDefinition Height="25"/> 
       <RowDefinition Height="15"/> 
      </Grid.RowDefinitions> 
      <Grid Grid.Row="1"> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="0.4*"/> 
        <ColumnDefinition Width="0.7*"/> 
       </Grid.ColumnDefinitions> 
       <Label Grid.Column="0" Name="lblstreet" Content="Street" VerticalAlignment="Top"></Label> 
       <TextBox Grid.Column="1" Name="txtStreet" VerticalAlignment="Stretch" Margin="0,0,0,5" TextChanged="txtStreet_TextChanged" Text="{Binding Path=szStreet Mode=OneWay}"></TextBox> 
      </Grid> 
      <Grid Grid.Row="2"> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="0.4*"/> 
        <ColumnDefinition Width="201"/> 
       </Grid.ColumnDefinitions> 
       <Label Grid.Column="0" Name="lblCity" Content="City" VerticalAlignment="Top"></Label> 
       <TextBox Grid.Column="1" Name="txtCity" VerticalAlignment="Top" Text="{Binding Path=szCityname,Mode=OneWay}"></TextBox> 
      </Grid> 
      <Grid Grid.Row="3"> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="0.4*"/> 
        <ColumnDefinition Width="0.7*"/> 
       </Grid.ColumnDefinitions> 
       <Label Grid.Column="0" Name="lblstate" Content="State/Province" VerticalAlignment="Top"></Label> 
       <TextBox Grid.Column="1" Name="txtState" VerticalAlignment="Top" Text="{Binding Path=szState}"></TextBox> 
      </Grid> 
      <Grid Grid.Row="4"> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="0.4*"/> 
        <ColumnDefinition Width="0.7*"/> 

       </Grid.ColumnDefinitions> 
       <Label Grid.Column="0" Name="lblZip" Content="Zip/PostalCode" VerticalAlignment="Top" Grid.ColumnSpan="2"></Label> 
       <TextBox Grid.Column="2" Name="txtZip" VerticalAlignment="Top" Text="{Binding Path=iZip}"></TextBox> 
      </Grid> 
      <Grid Grid.Row="5"> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="0.4*"/> 
        <ColumnDefinition Width="0.7*"/> 
       </Grid.ColumnDefinitions> 
       <Label Grid.Column="0" Name="lblCountry" Content="Country/Region" VerticalAlignment="Top"></Label> 
       <ComboBox Grid.Column="1" Name="cbCountry" VerticalAlignment="Top" IsEditable="True" ></ComboBox> 
      </Grid> 
     </Grid> 
    </GroupBox> 
    <StackPanel Grid.Row="1" Orientation="Horizontal" FlowDirection="LeftToRight"> 
     <CheckBox Name="chkFullAddress" Margin="5,8,5,5" Checked="CheckedEnabled" Unchecked="UncheckedEnabled" IsChecked="true"></CheckBox> 
     <Label Name="lblFullname" Content="Show this again when name is incomplete or unclear"></Label> 
    </StackPanel> 
    <StackPanel Grid.Row="2" Orientation="Horizontal" FlowDirection="RightToLeft"> 
     <Button Name="btnFullnameCancel" Content="Cancel" Margin="10,10,10,10" Width="50" Click="btnCheckAddressCancelClick"></Button> 
     <Button Name="btnFullnameOk" Content="Ok" Margin="10,10,10,10" Width="50" Click="btnCheckAddressOkClick"></Button> 
    </StackPanel> 
</Grid> 

Code Person.cs: Edité

public string szStreet 
    { 
     get ; 
     set ; 
    } 

    public string szCityname 
    { 
     get ; 
     set ; 
    } 
    public string szState { get; set; } 
    public int iZip { get; set; } 
    public static bool bCheck { get; set; } 
    public static bool bCheckFullAddress { get; set; } 

mainWindow LostFocusEvent: Edité

private void txtBox1_LostFocus(object sender, RoutedEventArgs e) 
    { 
     if ((!string.IsNullOrEmpty(txtBox1.Text)) && (!string.IsNullOrWhiteSpace(txtBox1.Text))) 
     { 
      if (Person.bCheckFullAddress) 
      { 
       CheckAddressWindow ca = new CheckAddressWindow(txtBox1); 

       ca.ShowDialog(); 
      } 
     } 

     else 
     { 
      CheckAddressWindow ca = new CheckAddressWindow(txtBox1); 
      ca.chkFullAddress.IsChecked = caObj.chkFullAddress.IsChecked; 
      ca.txtCity.Text = txtBox1.Text; 
      ca.ShowDialog(); 

     } 
    } 

événement ButtonClick: Edité

 private void button1_Click(object sender, RoutedEventArgs e) 
    { 

     caObj.chkFullAddress.IsChecked = (Person.bCheckFullAddress == true) ? true : false; 

     if (!string.IsNullOrEmpty(txtBox1.Text.Trim())) 
     { 
      CheckAddressWindow ca = new CheckAddressWindow(txtBox1); 
      ca.chkFullAddress.IsChecked = caObj.chkFullAddress.IsChecked; 
      ca.ShowDialog(); 

     } 

     else 
     { 
      CheckAddressWindow ca = new CheckAddressWindow(txtBox1); 
      ca.chkFullAddress.IsChecked = caObj.chkFullAddress.IsChecked; 
      ca.ShowDialog(); 
      ca.txtCity.Text = txtBox1.Text; 
     } 
    } 

CheckAddressWindow.cs: Edité

 public partial class CheckAddressWindow : Window 
{ 
    public static bool bChecked = true; 
    Person objPerson = new Person(); 
    bool bCheckAddress = true; 
    TextBox txt = new TextBox(); 
    public CheckAddressWindow() 
    { 
     InitializeComponent(); 
    } 

    public CheckAddressWindow(TextBox txtName) 
    { 
     InitializeComponent(); 
     txt = txtName; 
     StringCollection objSc = new StringCollection(); 
     int iLinecount = txt.LineCount; 
     for (int iCount = 0; iCount < iLinecount; iCount++) 
     { 
      objSc.Add(txt.GetLineText(iCount)); 
     } 
     if (objSc.Count.Equals(5)) 
     { 
      objPerson.szStreet = (objSc[0] + objSc[1]).Trim(); 
      objPerson.szCityname = objSc[2].Trim(); 
      objPerson.szState = objSc[3].Trim(); 
      objPerson.iZip = objSc[4].Trim(); 
     } 

     if (objSc.Count.Equals(3)) 
     { 

      objPerson.szStreet = (objSc[0] + objSc[1]).Trim(); 
      string[] arrName = objSc[2].Split(',', ' '); 
      objPerson.szCityname = arrName[0].Trim(); 
      objPerson.szState = arrName[1].Trim(); 
      objPerson.iZip = arrName[2].Trim(); 
     }  
    } 

    private void btnCheckAddressCancelClick(object sender, RoutedEventArgs e) 
    { 
     this.Close(); 
    } 

    private void btnCheckAddressOkClick(object sender, RoutedEventArgs e) 
    { 
     if (!string.IsNullOrEmpty(txtCity.Text)) 
     { 
      //txt.Text = txtStreet.Text + "\n" + txtCity.Text + "\n" + txtState.Text + "\n" + txtZip.Text + "\n" + cbCountry.SelectedValue; 
      objPerson.szStreet = txtStreet.Text.Trim(); 
      objPerson.szCityname = txtCity.Text.Trim(); 
      objPerson.szState = txtState.Text.Trim(); 
      objPerson.iZip = Convert.ToInt32(txtZip.Text.Trim()); 
      txt.Text = objPerson.szStreet + "\n" +objPerson.szCityname+","+txtState.Text + " " + txtZip.Text; 
      if (chkFullAddress.IsChecked == true) 
      { 
       bCheckAddress = true; 
      } 
      else 
      { 
       bCheckAddress = false; 
      } 
     } 
     this.Close(); 

    } 

    private void Window_Loaded_1(object sender, RoutedEventArgs e) 
    { 
     cbCountry.Items.Add("India"); 
     cbCountry.Items.Add("US"); 
     cbCountry.SelectedIndex = 0; 
    } 

    private void txtStreet_TextChanged(object sender, TextChangedEventArgs e) 
    { 

    } 

    private void CheckedEnabled(object sender, RoutedEventArgs e) 
    { 
     chkFullAddress.IsChecked = Person.bCheckFullAddress = true; 
    } 

    private void UncheckedEnabled(object sender, RoutedEventArgs e) 
    { 
     chkFullAddress.IsChecked = Person.bCheckFullAddress = false; 
    } 
} 

}

Je suis en train de lier des données ou texte saisi dans mainwindow à checkaddresswindowxaml .. .dès que le checkAddresswindow le charge, il montre que tous les champs de texte sont null, si je saisis des données n checkaddress feild puis cliquez sur le bouton ok, il montrera mainwindow.In mainWindow, lorsque vous cliquez sur le bouton il lie et montrant les deux valeurs de zone de texte.Mon problème pour la première fois ses données de liaison à checkAddresswindow.Comment puis-je atteindre this.am nouveau à Si je faisais des erreurs, rectifiez-moi. La liaison de données n'a pas lieu ... où je me suis trompé et quelle partie du code j'ai besoin de changer pour y arriver.

+0

@Sheridan Comment définir DataContext pour chaque zone de texte dans CheckAddressWindow.Can u pls moi bonne façon de lier dans WPF.any lien pour le comprendre correctement.am nouveau dans WF – kida

+0

@ Salutan Salut, j'ai édité le code ci-dessus.Je fonctionne maintenant comme je le demandais, mais pouvez-vous me suggérer une meilleure façon de lier des données dans WPF. – kida

Répondre

0

Lors de l'exécution de votre application, observez-vous des erreurs de liaison dans la fenêtre de sortie de débogage?

Je ne vois pas où vous définissez votre DataContext pour votre CheckAddressWindow. Je pense que vous avez coupé le haut de votre XAML pour ce fichier. Un autre problème peut être que l'objet "Personne" auquel vous vous liez dans votre CheckAddressWindow n'est pas le même objet "Personne" utilisé par MainWindow.

Je vous recommande d'avoir un modèle qui contient un objet Person auquel se lient MainWindow et CheckAddressWindow. De cette façon, les deux fenêtres se réfèrent à la même personne.

Pour info et tutoriels sur l'architecture MVVM pour WPF, consultez le lien suivant:

MVVM: Tutorial from start to finish?

+0

Salut, j'ai édité le code ci-dessus. Maintenant, il fonctionne comme je le souhaite, mais pouvez-vous me suggérer une meilleure façon de lier les données dans WPF.can upls me montrer quelques liens pour apprendre et mettre en œuvre la structure MVVM. – kida

+0

J'ai ajouté un lien ci-dessus pour vous aider avec les tutoriels pour MVVM. – Curtis

Questions connexes