不将数据从一个窗口的文本框绑定到另一个窗口的文本框

时间:2013-07-22 12:30:01

标签: wpf xaml c#-4.0 data-binding

数据从一个文本框绑定到另一个文本框是不正确的。这是下面的代码使用。

编辑:

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代码:已编辑

    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:已编辑

   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事件:已编辑

     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:被修改

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

}

我正在尝试将在mainwindow中输入的数据或文本绑定到checkaddresswindowxaml ...一旦checkAddresswindow加载它就会显示所有文本字段为空,如果我在checkaddress feild中输入一些数据然后单击ok按钮它将显示mainwindow 。在mainWindow中,当点击它绑定的按钮并显示两个文本框值。我的第一次发现它的绑定数据checkAddresswindow.How我可以实现this.am新概念。如果我没有犯错误请纠正我。 数据绑定不会发生......我出错了,我需要改变哪部分代码来实现它。

1 个答案:

答案 0 :(得分:0)

运行应用程序时,您是否在调试输出窗口中看到任何绑定错误?

我没有看到您为CheckAddressWindow设置DataContext的位置。我认为你切断了该文件的XAML顶部。

另一个问题可能是您在CheckAddressWindow中绑定的“Person”对象与MainWindow使用的“Person”对象不同。

我建议你有一个Model,它包含MainWindow和CheckAddressWindow都绑定的Person对象。这样,两个窗口都指的是同一个人。

有关WPF MVVM体系结构的信息和教程,请参阅以下链接:

MVVM: Tutorial from start to finish?