将数据源视图绑定到WPF中的Datagrid

本文关键字:WPF 中的 Datagrid 绑定 数据源 视图 | 更新日期: 2023-09-27 18:30:10

我正试图让我的数据显示在DataGrid中。我正在使用SQL Server 2012和Visual Studio 2010,并使用WPF应用程序。

我创建了一个新的数据源,选择了一个在SQL Server中创建的"视图"。我在数据窗格中选择了该视图上的下拉列表。我选择了下拉菜单并点击了DataGrid。然后我把它拖到一个用户控件上。当我运行应用程序时,会显示标题,但结果集不会。当我在SQL server中运行视图时,它会返回一个结果集。我做错了什么?

这是用户控件中的XAML

    <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>        

这是我的C#代码:

    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"];
        }
    }
}        

将数据源视图绑定到WPF中的Datagrid

为了进行数据绑定,您缺少了一些东西。

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的属性成为DB正在返回的对象的类型。此属性是DataGrid绑定到的属性。

例如:

private ObservableCollection<SomeDataType> _myPrivateData;
public ObservableCollection<SomeDataType> SomePropertyName { get { return _myPrivateData; } set    { _myPrivateData= value; NotifyPropertyChanged("SomePropertyName"); } }

它负责数据绑定部分。现在,每次重置DataGrid绑定到DataGrid的集合时,都会更新,因为调用了NotifyPropertyChanged。