【发布时间】:2021-10-11 22:09:15
【问题描述】:
由于 cmets 的反馈,我更新了我的问题。
我正在尝试根据SelectedValue/Item 在另一个ComboBox (cbLicenseHolder) 中对ComboBox (cbVehicle) 进行排序和填充。
与属性的绑定是通过BindableCollection 完成的(与ObservableCollection 基本相同)。
下面你会看到我的ViewModel。
namespace Ridel.Hub.ViewModels {
public class TripViewModel : Conductor<IScreen>.StackNavigation {
private readonly IWindowManager windowManager;
private BindableCollection<LicenseHolder> _licenseHolders;
public BindableCollection<LicenseHolder> LicenseHolders {
get => _licenseHolders;
set => SetAndNotify(ref this._licenseHolders, value);
}
private BindableCollection<License> _licenses;
public BindableCollection<License> Licenses {
get => _licenses;
set => SetAndNotify(ref this._licenses, value);
}
#region Constructor
public TripViewModel(IWindowManager windowManager) {
this.windowManager = windowManager;
LicenseHolders = new BindableCollection<LicenseHolder>();
Licenses = new BindableCollection<License>();
}
#endregion // Constructor
#region ComboBoxes
public void FillComboBoxLicenseHolders() {
try {
DataSet ds = new DataSet();
using (SqlConnection sqlCon = new SqlConnection(ConnectionString.connectionString)) {
SqlDataAdapter sqlDA = new();
sqlDA.SelectCommand = new SqlCommand("select Foretaksnavn from tblLicenseHolder", sqlCon);
sqlDA.Fill(ds);
}
DataTable dt = new();
dt = ds.Tables[0];
for (int i = 0; i < dt.Rows.Count; i++) {
DataRow dr = dt.NewRow();
dr = dt.Rows[i];
LicenseHolder licenseHolder = new();
licenseHolder.Foretaksnavn = dr["Foretaksnavn"].ToString();
LicenseHolders.Add(licenseHolder);
}
} catch (Exception ex) {
MessageBox.Show(ex.Message, "Message", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
public void FillComboBoxLicenses() {
Licenses.Clear();
try {
DataSet ds = new();
using (SqlConnection sqlCon = new SqlConnection(ConnectionString.connectionString)) {
SqlDataAdapter sqlDA = new();
sqlDA.SelectCommand = new SqlCommand("fillLicensesComboBox", sqlCon);
sqlDA.SelectCommand.CommandType = CommandType.StoredProcedure;
sqlDA.SelectCommand.Parameters.Add("@Foretaksnavn", SqlDbType.NVarChar).Value = CbLicenseHolderSelection.ToString();
sqlDA.Fill(ds);
}
DataTable dt = new();
dt = ds.Tables[0];
for (int i = 0; i < dt.Rows.Count; i++) {
DataRow dr = dt.NewRow();
dr = dt.Rows[i];
License license = new();
license.KjøretøyID = dr["KjøretøyID"].ToString();
Licenses.Add(license);
}
} catch (Exception ex) {
MessageBox.Show(ex.Message, "Message", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
private string _cbLicenseHolderSelection;
public string CbLicenseHolderSelection {
get { return this._cbLicenseHolderSelection; }
set { SetAndNotify(ref this._cbLicenseHolderSelection, value);
if (value != null) {
FillComboBoxLicenses();
}
}
}
如您所见,我有两个BindableCollection,一个名为LicenseHolders,另一个名为Licenses。 LicenseHolders 绑定到cbLicenseHolder,Licenses 绑定到cbVehicle。
另外,你看到我已经添加了我的方法; FillComboBoxLicenses,到CbLicenseHolderSelection 属性,在下面的XAML 中,你会看到我已经尝试将属性绑定到cbLicenseHolder 的SelectedValue。
但我显然在这里遗漏了一部分图片。 cbVehicle 没有被填充。
XAML 如下:
<ComboBox Name="cbLicenseHolder" Canvas.Left="50" Canvas.Top="204" Width="291"
ItemsSource="{Binding LicenseHolders, Mode=TwoWay}"
Loaded="{s:Action FillComboBoxLicenseHolders}"
DisplayMemberPath="Foretaksnavn"
SelectedValue="{Binding CbLicenseHolderSelection, UpdateSourceTrigger=PropertyChanged}"
FontSize="12"
/>
<ComboBox Name="cbVehicle" Canvas.Left="381" Canvas.Top="204" Width="269"
ItemsSource="{Binding Licenses, Mode=TwoWay}"
DisplayMemberPath="KjøretøyID"
FontSize="12"
IsEnabled="True"
IsSynchronizedWithCurrentItem="True"
/>
LicenseHolder.cs 和License.cs 都实现了PropertyChangedBase。
它们的属性如下所示:
private string _foretaksnavn;
public string Foretaksnavn {
get { return this._foretaksnavn; }
set { SetAndNotify(ref this._foretaksnavn, value); }
}
SetAndNotify 是一个框架函数 - 它这样做:
"通过引用获取一个字段及其新值。如果字段 != 值, 将设置 field = value 并引发 PropertyChanged 通知。”
我的stored procedure:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[getLicense]
@Foretaksnavn nvarchar(50)
AS
SELECT tblLicense.ID, LøyvehaverID, KjøretøyID
FROM tblLicense
INNER JOIN tblLicenseHolder
ON tblLicense.LøyvehaverID = tblLicenseHolder.Foretaksnavn
WHERE tblLicense.LøyvehaverID = @Foretaksnavn
【问题讨论】:
-
旁注:SQL 操作不属于 ViewModel。
-
您可以删除所有 SQL 代码(保持问题简短)。我们只需要知道列表不是空的。
-
Licenses应该是一个 BindingList 和/或您需要在填写后调用 PropertChanged(nameof(Licenses))。 -
另外请不要把两个问题合二为一。改为问两个问题。
-
我建议为每个问题创建一个问题。
标签: c# sql-server wpf mvvm