【问题标题】:Binding Datasource View to Datagrid in WPF在 WPF 中将数据源视图绑定到数据网格
【发布时间】:2014-11-11 17:53:54
【问题描述】:

我正在尝试让我的数据显示在 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"];
        }
    }
}        

【问题讨论】:

  • 为什么不将数据放到 ObservableCollection 并将这个集合绑定到数据网格的源?

标签: c# wpf xaml datagrid


【解决方案1】:

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

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) 为 DB 返回的对象类型创建一个 ObservableCollection 类型的属性。此属性是 DataGrid 绑定到的属性。

例如:

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

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

【讨论】:

  • 这很有帮助。感谢您抽出宝贵时间。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-31
  • 2017-05-14
  • 2015-10-06
  • 1970-01-01
  • 2011-01-01
  • 1970-01-01
相关资源
最近更新 更多