当前位置:首页 » 数据仓库 » wpf数据库绑定
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

wpf数据库绑定

发布时间: 2022-12-18 05:01:36

⑴ WPF数据绑定问题(很菜的)

如果这个实体会被别的访问者改变,并且需要通知UI,或者UI数据改变后需要通知实体的话就需要实现INotifyPropertyChanged接口,如果实体从数据库查询出来后,界面只是展示数据,这就不需要了。ObservableCollection<T>是实现了INotifyPropertyChanged的集合,建议你看看msdn上的说明。另外如果需要实现的话建议你当前的架构使用MVVM模式。

⑵ Wpf的comboBox怎么绑定数据

WPF中提供了数据绑定的功能,操作起来很方便,集合类的控件几乎都可以用数据源来进行数据的绑定,下面操作一下下拉列表框控件ComboBox控件的数据绑定操作。
要绑定到ComboBox控件的自定义类:
public class LocationRoad
{
public int ID { set; get; }
public string Code { set; get; }
public string Info { set; get; }
}
建立数据源,就将此数据集合当作数据源绑定到ComboBox:
///
/// 当ComboBox选中项更改时发生
///
private LocationRoad _selectLocation;
public LocationRoad SelectLocation
{
get
{
return this._selectLocation;
}
set
{
this._selectLocation = value;
if (this.PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("SelectLocation"));
}
}

private ObservableCollection _locationRoad = null;

public ObservableCollection LocationSource
{
get
{
if (this._locationRoad == null)
{
this._locationRoad = new ObservableCollection() {
new LocationRoad() { ID = 1, Code = "NGQ", Info = "南岗区" },
new LocationRoad() { ID = 2, Code = "DLQ", Info = "道里区" },
new LocationRoad() { ID = 3, Code = "DWQ", Info = "道外区" },
new LocationRoad() { ID = 4, Code = "PFQ", Info = "平房区" },
new LocationRoad() { ID = 5, Code = "XFQ", Info = "香坊区" },
};

}
return this._locationRoad;
}
set
{
this._locationRoad = value;
if (this.PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("LocationSource"));
}
}

⑶ wpf数据绑定source和elementname的区别

在WPF的数据绑定中:

  • Source 用于指定数据源为后台数据对象、集合

  • ElementName 用于指定“数据源”为界面上某个控件的某个属性


例如,以下xaml中

<TextBoxx:Name="txtName"/>
<TextBlockText="{BindingElementName=txtName,Path=Text}"/>

通过ElementName,TextBlock的Text属性被绑定另一个名为txtName控件上的Text属性上(由Path指定)

⑷ wpf绑定显示两位小数

关于c#:在WPF绑定中设置小数属性的默认值

c#mvvmwpf
Setting the Default Value for Decimal Property in WPF Binding
我将WPF表单绑定到类的Decimal属性。 如果用户输入无效的格式(字符串而不是十进制),则文本框会自动以红色显示。 但是,我想通过在将插入的数据存储到数据库之前进行验证来使其更加安全。

问题是,每当用户输入非十进制值时,绑定将返回0,而不是null或error。 因此,它无需第二级验证就可以进入数据库。

验证WPF与小数的绑定的最佳方法是什么? 现在它不会返回null,所以我没有任何办法捕获错误。

这是我绑定文本框的方式

1
        <TextBox x:Name="stockTxtBx" Grid.Row="3" Grid.Column="1"  Style="{StaticResource StandardBox}" Text="{Binding StockOnHand}"/>
另外,在哪里可以修改以添加验证?

相关讨论
我确定此页面会为您提供帮助
@HaikalNashiha也许谷歌翻译
@HaikalNashiha此页面也很适合您
The problem is, whenever a user enter a non decimal value, the binding will return 0 instead of null or error

您在上述声明中略有错误。当用户输入某些文本的特定字段的类型无效时,实际发生的情况是:

无效的文本会导致TextBox周围出现红色边框(或其他修饰,具体取决于ErrorTemplate值)。
数据绑定属性值保留为最后输入的有效值
因此,在您的情况下,最后一个有效值可能是0,这就是为什么您认为无效值将始终返回0的原因。因此,实际上,只有无效值被忽略,而最后一个有效值仍然保留。

但是,要解决此问题,您有几种选择。一种方法是在保存数据之前检查Validation.HasError附加属性的值。显然,如果您检测到存在任何错误,那么您将弹出一条消息来提醒用户,而不是继续保存。您可以从MVVM问题中的Binding Validation.HasError属性中找到有关此方法的更多信息。

另一个选择是限制特定TextBox的文本输入,这样就不可能输入非数字键。在这里,我不会再详细说明如何执行此操作,而是希望您在Stack Overflow上要求您查看

⑸ wpf中如何将datagrid控件绑定到数据库

给你找了个例子,是直接绑定到数据库里的table的
后台代码:
MKP.MyCommon.sqlHelper sh = new MKP.MyCommon.SQLHelper(null);
DataTable dt=sh.GetDataTable("select Fi_id,Fs_pCompanyName,Fs_phone,Fs_email from TpmCompany");
dataGrid1.ItemsSource = dt.DefaultView;
前台代码:
<pre class="csharp" name="code"><DataGrid AutoGenerateColumns="False"
Height="153" HorizontalAlignment="Left" Name="dataGrid1" VerticalAlignment="Top"
Width="449" SelectedCellsChanged="dataGrid1_SelectedCellsChanged">
<DataGrid.Columns >
<DataGridTextColumn Header="ID" Binding="{Binding Path=Fi_id}"/>
<DataGridTextColumn Header="公司名称" Binding="{Binding Path=Fs_pCompanyName}"/>
<DataGridTextColumn Binding="{Binding Path=Fs_phone}" Header="电话" />
<DataGridTextColumn Binding="{Binding Path=Fs_email}" Header="Email"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>

⑹ WPF的DataGrid自动绑定数据库时最后的空列到底要怎么消除

将AutoGenerateColumns设置为false。然后在代码中手动生成不同表相对应的百分比列宽的与数据库表的列名对应的列。其余部分不变即可。

代码:

string attr = "";
dataDataGrid.AutoGenerateColumns = false;

MultiSecurity ms = new MultiSecurity();
OracleConnection oraCon = ms.CreateConnection("msdb", "maple", "manager");

foreach (string att in ms.getAttributes(tablename, oraCon)) //获取指定表的所有列
{
attr += att + ",";
dataDataGrid.Columns.Add(new DataGridTextColumn() { Header = att, Binding = new Binding(att), Width = new DataGridLength(2, DataGridLengthUnitType.Star) }); //为DataGrid生成百分比列宽的列,相当于xaml中设置Width = "2*"
}

attr = attr.Substring(0, attr.Length - 1);
sql = "select "+attr+" from " + node.Name; //生成查询指定列的sql语句
DataTable dt = ms.getData(sql, oraCon);
dataDataGrid.ItemsSource = dt.DefaultView; //将结果集绑定到DataGrid