从一个文本框到另一个文本框的数据绑定没有正确发生。这是正在使用的以下代码。
编辑:
主窗口xml:
<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; }
主窗口 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;
}
}
}
我正在尝试将在主窗口中输入的数据或文本绑定到 checkaddresswindowxaml ......一旦 checkAddresswindow 加载它显示所有文本字段为空,如果我在 checkaddress 字段中输入一些数据然后单击确定按钮它将显示主窗口。在主窗口,当单击它绑定并显示两个文本框值的按钮时。我的问题是第一次绑定数据到检查地址窗口。我怎样才能实现这个。我是这个概念的新手。如果我没有错误,请纠正我。没有发生数据绑定......我哪里出错了,我需要更改哪部分代码来实现它。