具有绑定到 Datagrid 的 ObservableCollection 的 DependencyProperty
我正在尝试学习 WPF 中的新东西,但我无法让它发挥作用。我已经声明了一个 DependencyProperty,并且我正在尝试使用它来将可观察集合绑定到数据网格。 这是我的代码:
public static readonly DependencyProperty dpActivityProperty =
DependencyProperty.Register("dpActivity",
typeof(ObservableCollection<Activity>),
typeof(MainWindow));
public ObservableCollection<Activity> dpActivity
{
get { return (ObservableCollection<Activity>)GetValue(dpActivityProperty); }
set { SetValue(dpActivityProperty, value); }
}
这是我的 xaml:
<WpfToolkit:DataGrid Name="DG1" ItemsSource="{Binding dpActivity,ElementName=Utils}"
Utils 是主窗口的 x:Name。
我遇到的问题是我的数据网格中没有显示任何数据。不存在绑定错误,如果不使用依赖属性并将 itemssource 设置为可观察集合,我就可以看到数据。我尝试用谷歌搜索这个,但没有成功。有人能指出我正确的方向吗?
我在项目上放置了另一个数据网格,并设置 autogeneratecolumns=true。这将显示来自 dpActivity 的数据。错误一定是在这段代码中:
<WpfToolkit:DataGrid.Columns>
<WpfToolkit:DataGridTextColumn
Header="Sport"
Width="90"
Binding="{Binding Path=Sport}" />
仍然没有绑定错误。
哇!很抱歉我浪费了大家的时间,我发现了一个 DG1.ItemsSource = Null 语句,我忘了注释掉。这超越了一切,给了我一个空的数据网格。现在一切都很好。再次抱歉
I am trying to learn a new thing in WPF, and I just can't get it to work. I have declared a DependencyProperty, and I am trying to use it to bind a observable collection to a datagrid.
This is my code:
public static readonly DependencyProperty dpActivityProperty =
DependencyProperty.Register("dpActivity",
typeof(ObservableCollection<Activity>),
typeof(MainWindow));
public ObservableCollection<Activity> dpActivity
{
get { return (ObservableCollection<Activity>)GetValue(dpActivityProperty); }
set { SetValue(dpActivityProperty, value); }
}
Here is my xaml:
<WpfToolkit:DataGrid Name="DG1" ItemsSource="{Binding dpActivity,ElementName=Utils}"
Utils is the x:Name of the mainwindow.
The problem I am having is that no data is displayed in my datagrid. There are no binding errors, and I can see the data if I don't use the dependency property and just set the itemssource to the observable collection. I have tried to google this, and no luck. Can anyone point me in the right direction?
I put another datagrid on the project, and set autogeneratecolumns=true. This displays the data from dpActivity. the error must be in this code:
<WpfToolkit:DataGrid.Columns>
<WpfToolkit:DataGridTextColumn
Header="Sport"
Width="90"
Binding="{Binding Path=Sport}" />
There are still no binding errors.
Wow! I am sorry I wasted everyone's time, I found a DG1.ItemsSource = Null statement that I forgot to comment out. That overrode everything to give me a empty datagrid. Everything works great now. Sorry again
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论