wpf绑定数据库
A. WPF TreeView绑定数据
我给你我自己的源码你试试看,但是我是用递归来实现添加删除增加展开子节点的
B. wpf控件属性 数据绑定到access的数据库(.accdb格式)。
<Label x:Name="Label" Content="{Binding Name}"></Label> 然后在设置DataContext
C. WPF中 Datagrid 绑定到数据库后如何对用户输入的数据进行验证
使用ValidationRule。以下为示例:
Binding testBinding = new Binding();
testBinding.ValidationRules.Add(new IPv4ValidationRule());//添加验证,根据数据类型添加不一样的验证类
testBinding.Mode = BindingMode.TwoWay;
testBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
testBinding.Path = new PropertyPath("[" + myTextBox.Name + "].Permit");//绑定源路径
testBinding.Source = dic; //绑定源,
myTextBox.SetBinding(TextBox.TextProperty ,testBinding );//绑定目标属性
数据验证也是加的数据验证类继承ValidationRule。
public class IPv4ValidationRule : ValidationRule
{
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
var str = value as string;
if (String.IsNullOrEmpty(str))
{
MessageBox.Show("请输入日期。");
//return new ValidationResult(false,
// "Please enter an IP Address.");
}
return new ValidationResult(true, null);
}
}
D. wpf中如何将datagrid控件绑定到数据库上呀
xaml中绑定:
<DataGrid Name="dataGrid1" >
<DataGridTextColumn Header="日期" Binding="{Binding Path=Date}"/>//table表中的字段
</DataGrid>
xaml.cs:
string con = "Provider=System.Data.sqlClient;Data Source=.;database=logistics;User ID=sa;Password=sa123";
SqlConnection sCon = new SqlConnection(con);
sCon .Open();
string sql = "select * from table";
DataTable dt = new DataTable();
SqlDataAdapter da=new SqDataAdapter(sql,sCon );
DataSet ds=new DataSet();
ds.Fill(ds,"dtName");
dataGrid1.ItemsSource = ds.Tables["dtName"].DefaultView;
E. 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>
F. wpf 中的combox 如何绑定数据库字段到text value
将读出来的的datatable赋值给combobox的上下文或者数据源(Itemsource),然后将DisplayPath这个设置为你需要的那列的名字