0

Data Binding from one textbox to another is not happen properly .Here is the below code am using.

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>

Person.cs code:Edited

    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: Edited

   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();

        }
    }

ButtonClick event:Edited

     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:Edited

        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;
    }
}

}

I am trying to bind data or text entered in mainwindow to checkaddresswindowxaml...as soon as the checkAddresswindow loads it shows all the text feilds are null,if i enter some data in checkaddress feild then click ok button it will show mainwindow.In mainWindow ,when click the button it binding and showing the two text box values.My issue for the first time its binding data to checkAddresswindow.How can i achieve this.am new to this concepts.If i did nay mistakes please rectify me. Data binding is not happen...where i went wrong and which part of code i need to change to achieve it.

kida
  • 167
  • 2
  • 3
  • 15
  • @Sheridan how can i set DataContext for each textbox in CheckAddressWindow.Can u pls let me know correct way of binding in WPF.any link to understand it properly.am new in WF – kida Jul 22 '13 at 16:45
  • @sheridan hi, i have edited the above code.now it works as i required,but can u suggest me better way of binding data in WPF. – kida Jul 22 '13 at 18:08

1 Answers1

0

When running your app, do you see any Binding Errors in your debug output window?

I am not seeing where you are setting your DataContext for your CheckAddressWindow. I think you cut off the top of your XAML for that file.

Another problem may be that the "Person" object you are binding to in your CheckAddressWindow is not the same "Person" object being used by the MainWindow.

I would recommend that you have a Model that contains a Person object that both the MainWindow and the CheckAddressWindow bind to. This way, both windows are referring to the same person.

For info and tutorials on MVVM architecture for WPF, see the following link:

MVVM: Tutorial from start to finish?

Community
  • 1
  • 1
Curtis
  • 5,794
  • 8
  • 50
  • 77
  • hi, i have edited the above code .now it works as i required,but can u suggest me better way of binding data in WPF.can u pls show me some links to learn and implement the MVVM structure. – kida Jul 22 '13 at 18:08
  • I have added a link above to help you with tutorials for MVVM. – Curtis Jul 22 '13 at 20:13