项目主题: 我有一个连接到 SQL Server 数据库的 MS Access 2003 adp VBA 项目,现在我正在将该项目转换为 C# WPF,并且我打算将数据从 C# WPF 中的 DataGrid 直接保存到 SQL Server 数据库
细节: 我有 DataGrid,ItemsSource 绑定到名为“INVO_DATA_RASID_KHARID”的“ObservableCollection”以及名为“ANBAR_COLUMN”的“DataGridComboBoxColumn”,此“DataGridComboBoxColumn”的“ItemSource”通过 SQL Server 填充。
我希望此“DataGridComboBoxColumn”以默认值显示,以便用户知道无需从“DataGridComboBoxColumn”列表中手动选择它。
我通过这个名为“FillALLComboboxes”的方法后面的代码中的静态变量设置这个默认值。
问题:考虑到DataGrid中的'DataGridComboBoxColumn','ANBAR_COL''DataGridComboBoxColumn'有一个默认值,用户不需要从'DataGridComboBoxColumn'列表中手动选择那个项目,但只要他们加倍-点击'ANBAR_COL'组合框单元格,默认值就会出现! 重点是为“DataGridComboBoxColumn”设置的默认值在用户输入单元格之前不会显示!
XAML:
<DataGrid x:Name="INVO_LST_RASID_SUB"
EnableColumnVirtualization="True"
EnableRowVirtualization="True"
VirtualizingPanel.IsVirtualizing="True"
VirtualizingPanel.VirtualizationMode="Recycling"
ScrollViewer.CanContentScroll="False"
KeyboardNavigation.TabNavigation="Contained"
ItemsSource="{Binding INVO_DATA_RASID_KHARID}"
Grid.Row="5"
Margin="5,10"
AutoGenerateColumns="False" FlowDirection="RightToLeft" SelectionUnit="FullRow" BorderThickness="1" >
<DataGrid.Columns>
<DataGridComboBoxColumn x:Name="ANBAR_COLUMN" MinWidth="80" Width="auto" Header=" انبار "
SelectedValueBinding="{Binding ANBAR,UpdateSourceTrigger=PropertyChanged}"
DisplayMemberPath="NAMES" SelectedValuePath="CODE">
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="IsEditable" Value="True"/>
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
</DataGrid.Columns>
</DataGrid>
我的窗口后面的代码(C#):
//MainWindow:
public partial class HEAD_LST_RASID : Window
{
public ObservableCollection<INVO_LST_RASID_KHARID_CSHARP> INVO_DATA_RASID_KHARID { get; set; } = new ObservableCollection<INVO_LST_RASID_KHARID_CSHARP>();
public HEAD_LST_RASID()
{
InitializeComponent();
this.DataContext = this;
}
public void FillALLComboboxes()
{
string RowSource_ANBAR = "SELECT CODE, NAMES FROM dbo.TCOD_ANBAR";
//Search for the default value if it exists
var rst = dbms.Database.SqlQuery<int?>("SELECT ANBCO FROM dbo.OPANBACCESS WHERE (USERCO = " + Baseknow.USERCOD + " ) ORDER BY dbo.OPANBACCESS.RDF").ToList();
if (rst.Count > 0)
{
Baseknow.anbardef = (int)rst.FirstOrDefault();
}
var ARST = dbms.Database.SqlQuery<Custom_TCODANBAR>(RowSource_ANBAR).ToList();
ANBAR_COLUMN.ItemsSource = ARST;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
FillALLComboboxes();
var QRE_LST = dbms.Database.SqlQuery<INVO_LST_RASID_KHARID_CSHARP>($@"SELECT * FROM INVOICES").ToList();
INVO_DATA_RASID_KHARID?.Clear();
foreach (var item in QRE_LST)
INVO_DATA_RASID_KHARID.Add(item);
}
}
模特班:
//Model:
public class CTABLES
{
public class INVO_LST_RASID_KHARID_CSHARP : INotifyPropertyChanged, ICloneable
{
public object Clone()
{
return this.MemberwiseClone();
}
public INVO_LST_RASID_KHARID_CSHARP()
{
ANBAR = Baseknow.anbardef;
}
private int? _anbar;
public int? ANBAR
{
get
{
return _anbar;
}
set
{
if (_anbar == value)
return;
_anbar = value;
OnPropertyChanged("ANBAR");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
我想要的是在加载“DataGrid”数据时显示已为“DataGrid”中的此组合框选择了默认值(无需双击并进入CellEditing模式)。
我绕过了我的问题并且成功了! :
XAML:
<DataGridComboBoxColumn x:Name="ANBAR_COLUMN"
DisplayMemberPath="NAMES" SelectedValuePath="CODE"
SelectedValueBinding="{Binding ANBAR,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"
MinWidth="80" Width="auto" Header=" انبار "
>
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="IsEditable" Value="True"/>
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<Setter Property="SelectedIndex" Value="{Binding DefaultValDisplay, Source={StaticResource BoundyAnbar}}"/>
</Style>
</DataGridComboBoxColumn.ElementStyle>
</DataGridComboBoxColumn>
C#:
using System.ComponentModel;
namespace WpfApp5.Boundy
{
public class CL_Boundy : INotifyPropertyChanged
{
private int _myPropertyVariable = 0;
public int DefaultValDisplay
{
get { return _myPropertyVariable; }
set
{
_myPropertyVariable = value;
OnPropertyChanged(nameof(DefaultValDisplay));
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}