WPF 在更新时更改绑定值
本文关键字:绑定 更新 WPF | 更新日期: 2023-09-27 17:55:16
我有一个列表视图,如下所示:
<ListView ItemsSource="{Binding OrganizationInfo}">
<DockPanel>
<StackPanel Width="130">
<!-- Name -->
<Label Content="Organization Name" VerticalContentAlignment="Center" HorizontalContentAlignment="Right" Margin="4" Height="25" />
<!-- Street 1-->
<Label Content="Address 1" VerticalContentAlignment="Center" HorizontalContentAlignment="Right" Margin="4" Height="25" />
<!-- Street 2-->
<Label Content="Address 2" VerticalContentAlignment="Center" HorizontalContentAlignment="Right" Margin="4" Height="25" />
<!-- City -->
<Label Content="City" VerticalContentAlignment="Center" HorizontalContentAlignment="Right" Margin="4" Height="25" />
<!-- State -->
<Label Content="State" VerticalContentAlignment="Center" HorizontalContentAlignment="Right" Margin="4" Height="25" />
<!-- Zip -->
<Label Content="Zip" VerticalContentAlignment="Center" HorizontalContentAlignment="Right" Margin="4" Height="25" />
<!-- County -->
<Label Content="County" VerticalAlignment="Center" HorizontalContentAlignment="Right" Margin="4" Height="25" />
<!-- Service Area -->
<Label Content="Service Area" VerticalAlignment="Center" HorizontalContentAlignment="Right" Margin="4" Height="25" />
</StackPanel>
<StackPanel Width="330">
<ComboBox Name="OrgNameComboBox" ItemsSource="{Binding Organizations}" SelectedValue="{Binding OrganizationName}" VerticalContentAlignment="Center" Margin="0 4" Height="25" />
<TextBox Name="OrgStreet1TextBox" Text="{Binding OrgStreet1}" VerticalContentAlignment="Center" Margin="0 4" Height="25" />
<TextBox Name="OrgStreet2TextBox" Text="{Binding OrgStreet2}" VerticalContentAlignment="Center" Margin="0 4" Height="25" />
<TextBox Name="OrgCityTextBox" Text="{Binding OrgCity}" VerticalContentAlignment="Center" Margin="0 4" Height="25" />
<TextBox Name="OrgStateTextBox" Text="{Binding OrgState}" VerticalContentAlignment="Center" HorizontalAlignment="Left" Margin="0 4" Width="30" Height="25" />
<TextBox Name="OrgZipTextBox" Text="{Binding OrgZip}" VerticalContentAlignment="Center" HorizontalAlignment="Left" Margin="0 4" Width="60" Height="25" />
<TextBox Name="OrgCountyTextBox" Text="{Binding OrgCounty}" VerticalContentAlignment="Center" Margin="0 4" IsEnabled="False" Height="25" />
<TextBox Name="OrgServiceAreaTextBox" Text="{Binding OrgServiceArea}" VerticalContentAlignment="Center" Margin="0 4" IsEnabled="False" Height="25" />
</StackPanel>
</DockPanel>
</ListView>
它通过视图模型绑定:
class OutreachEventViewModel
{
//SQL TableAdapters & DataTables
internal readonly OrganizationListTableAdapter OrganizationListTableAdapter = new OrganizationListTableAdapter();
internal readonly OrgByNameTableAdapter OrgByNameTableAdapter = new OrgByNameTableAdapter();
public ObservableCollection<string> Organizations { get; set; }
public string OrganizationName { get; set; }
//ViewModel that populates the contact listbox
public OutreachEventViewModel()
{
//Perform SQL querys
var organizations = OrganizationListTableAdapter.GetOrganizationList();
//Declare needed variables
var organizationList = new ObservableCollection<string>();
//Store SQL results into lists
organizationList.AddRange((IEnumerable)(from DataRow row in organizations.Rows select row.ItemArray[0].ToString()));
Organizations = organizationList; //Set list as ItemsSource
}
//Some method to update Organization address information
public class OrgAddress : ViewModelBase
{
private string _street1;
private string _street2;
private string _city;
private string _state;
private string _zip;
private string _county;
private string _serviceArea;
public string OrgStreet1
{
get { return _street1; }
set
{
if (_street1 != value)
{
Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "OrgStreet1" });
_street1 = value;
OnPropertyChanged("OrgStreet1");
}
}
}
public string OrgStreet2
{
get { return _street2; }
set
{
if (_street2 != value)
{
Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "OrgStreet2" });
_street2 = value;
OnPropertyChanged("OrgStreet2");
}
}
}
public string OrgCity
{
get { return _city; }
set
{
if (_street1 != value)
{
Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "OrgCity" });
_city = value;
OnPropertyChanged("OrgCity");
}
}
}
public string OrgState
{
get { return _state; }
set
{
if (_state != value)
{
Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "OrgState" });
_state = value;
OnPropertyChanged("OrgState");
}
}
}
public string OrgZip
{
get { return _zip; }
set
{
if (_zip != value)
{
Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "OrgZip" });
_zip = value;
OnPropertyChanged("OrgZip");
}
}
}
public string OrgCounty
{
get { return _county; }
set
{
if (_county != value)
{
Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "OrgCounty" });
_county = value;
OnPropertyChanged("OrgCounty");
}
}
}
public string OrgServiceArea
{
get { return _serviceArea; }
set
{
if (_serviceArea != value)
{
Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "OrgServiceArea" });
_serviceArea = value;
OnPropertyChanged("OrgServiceArea");
}
}
}
}
}
每当组织名称更改时,我希望它通过查询SQL然后设置绑定(OrgStreet1,OrgStreet2,OrgCity,OrgState,OrgZip,OrgCounty,OrgServiceArea)来更新其余字段。
但是我不知道该怎么做。
我希望地址字段保持为空,只要没有选择任何组织。
可以将代码放在 OrganizationName 属性的 CLR 资源库上,以根据需要更新其余属性:
private string organizationName;
public string OrganizationName
{
get { return organizationName; }
set
{
if (organizationName != value)
{
organizationName = value;
UpdateOrganizationData();
OnPropertyChanged("OrganizationName");
}
}
}
然后,只需在 UpdateOrganizationData
方法中查询新信息,即可设置。