将数据源视图绑定到WPF中的Datagrid [英] Binding Datasource View to Datagrid in WPF
问题描述
我创建了一个新的数据源,我选择了在SQL Server中创建的View 。我在数据窗格中选择了该视图的下拉列表。我选择了下拉列表并点击了DataGrid。然后我将其拖动到用户控件。当我运行应用程序时,标题显示,但结果集不显示。当我在SQL Server中运行视图时返回一个结果集。我做错了什么?
这是用户控制中的XAML
< UserControl.Resources>
< CollectionViewSource x:Key =myviewsViewSourced:DesignSource ={d:DesignInstance my:myview,CreateList = True}/>
< /UserControl.Resources>
< Grid DataContext ={StaticResource myviewsViewSource}>
< DataGrid AutoGenerateColumns =FalseEnableRowVirtualization =TrueHeight =200HorizontalAlignment =LeftItemsSource ={Binding}Margin =561,121,0,0Name =myviewsDataGridRowDetailsVisibilityMode = VisibleWhenSelectedVerticalAlignment =TopWidth =400>
< DataGrid.Columns>
< DataGridTextColumn x:Name =mnemonicColumnBinding ={Binding Path = Mnemonic}Header =助记符Width =SizeToHeader/>
< DataGridTextColumn x:Name =nameColumnBinding ={Binding Path = Name}Header =NameWidth =SizeToHeader/>
< DataGridTextColumn x:Name =toolColumnBinding ={Binding Path = Tool}Header =ToolWidth =SizeToHeader/>
< DataGridTextColumn x:Name =filterColumnBinding ={Binding Path = Filter}Header =FilterWidth =SizeToHeader/>
< DataGridTemplateColumn x:Name =createdColumnHeader =CreatedWidth =SizeToHeader>
< DataGridTemplateColumn.CellTemplate>
< DataTemplate>
< DatePicker SelectedDate ={Binding Path = Created,Mode = TwoWay,ValidatesOnExceptions = true,NotifyOnValidationError = true}/>
< / DataTemplate>
< /DataGridTemplateColumn.CellTemplate>
< / DataGridTemplateColumn>
< DataGridTextColumn x:Name =typeColumnBinding ={Binding Path = Type}Header =TypeWidth =SizeToHeader/>
< /DataGrid.Columns>
< / DataGrid>
< / Grid>
这是我的C#代码:
public partial class ScanControl:UserControl
{
public ScanControl()
{
InitializeComponent();
}
private void UserControl_Loaded_1(object sender,RoutedEventArgs e)
{
if(!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
{
//在此加载您的数据并将结果分配给CollectionViewSource。
System.Windows.Data.CollectionViewSource myCollectionViewSource =(System.Windows.Data.CollectionViewSource)this.Resources [myviewsViewSource];
}
}
}
1)确保将DataContext设置为 DataContext = {Binding RelativeSource = {RelativeSource Self}}
2)您需要将DataGrid中的ItemsSource绑定更改为: ItemsSource ={Binding SomePropertyName}
3)您需要实现INotifyPropertyChanged
4)要实现此属性,请使用以下内容:
#region INotifyPorpertyChanged Memebers
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string propertyName =)
{
if(PropertyChanged!= null)
PropertyChanged(this,new PropertyChangedEventArgs(propertyName));
}
#endregion
5)键入数据库返回的对象类型的 ObservableCollection
。该属性是DataGrid绑定的属性。
例如:
private ObservableCollection< SomeDataType> _myPrivateData;
public ObservableCollection< SomeDataType> SomePropertyName {get {return _myPrivateData; } set {_myPrivateData = value; NotifyPropertyChanged(SomePropertyName); }}
它负责处理数据绑定部分。现在每当您重置DataGrid绑定到您的DataGrid的集合时,都会更新,因为调用了NotifyPropertyChanged。
I am trying to get my data to show in a DataGrid. I am using SQL Server 2012 and Visual Studio 2010 and working with a WPF application.
I created a new datasource, I chose a "View" that I created in SQL Server. I selected the dropdown on that View in the data pane. I chose the dropdown and clicked on DataGrid. Then I dragged it to a user control. When I run the application, the headers show but the result set does not. When I run the view in SQL server it returns a result set. What am I doing wrong?
This is the XAML in the User Control
<UserControl.Resources>
<CollectionViewSource x:Key="myviewsViewSource" d:DesignSource="{d:DesignInstance my:myview, CreateList=True}" />
</UserControl.Resources>
<Grid DataContext="{StaticResource myviewsViewSource}">
<DataGrid AutoGenerateColumns="False" EnableRowVirtualization="True" Height="200" HorizontalAlignment="Left" ItemsSource="{Binding}" Margin="561,121,0,0" Name="myviewsDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" VerticalAlignment="Top" Width="400">
<DataGrid.Columns>
<DataGridTextColumn x:Name="mnemonicColumn" Binding="{Binding Path=Mnemonic}" Header="Mnemonic" Width="SizeToHeader" />
<DataGridTextColumn x:Name="nameColumn" Binding="{Binding Path=Name}" Header="Name" Width="SizeToHeader" />
<DataGridTextColumn x:Name="toolColumn" Binding="{Binding Path=Tool}" Header="Tool" Width="SizeToHeader" />
<DataGridTextColumn x:Name="filterColumn" Binding="{Binding Path=Filter}" Header="Filter" Width="SizeToHeader" />
<DataGridTemplateColumn x:Name="createdColumn" Header="Created" Width="SizeToHeader">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DatePicker SelectedDate="{Binding Path=Created, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn x:Name="typeColumn" Binding="{Binding Path=Type}" Header="Type" Width="SizeToHeader" />
</DataGrid.Columns>
</DataGrid>
</Grid>
Here is my C# code:
public partial class ScanControl : UserControl
{
public ScanControl()
{
InitializeComponent();
}
private void UserControl_Loaded_1(object sender, RoutedEventArgs e)
{
if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
{
//Load your data here and assign the result to the CollectionViewSource.
System.Windows.Data.CollectionViewSource myCollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["myviewsViewSource"];
}
}
}
You are missing a few things in order to data bind.
1) Make sure you have set your DataContext to DataContext="{Binding RelativeSource={RelativeSource Self}}"
2) You need to change the ItemsSource binding in your DataGrid to: ItemsSource="{Binding SomePropertyName}"
3) You need to implement `INotifyPropertyChanged'
4) To implement this property use the following:
#region INotifyPorpertyChanged Memebers
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string propertyName = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
5) Make an property of type ObservableCollection
of the type of object the the DB is returning. This property is the property the DataGrid is bounded to.
ex:
private ObservableCollection<SomeDataType> _myPrivateData;
public ObservableCollection<SomeDataType> SomePropertyName { get { return _myPrivateData; } set { _myPrivateData= value; NotifyPropertyChanged("SomePropertyName"); } }
That takes care of the data binding part. Now every time you reset the collection that DataGrid is bound to your DataGrid will update because the NotifyPropertyChanged is called.
这篇关于将数据源视图绑定到WPF中的Datagrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!