WPF DataGrid没有填充viewmodel中的信息(DataBinding无法正常工作)

时间:2015-02-05 20:40:21

标签: wpf xaml mvvm datagrid

我有一个名为UserControl的{​​{1}},以及一个TagGridView,其中TagGridViewModel使用数据绑定显示在ObservableCollection<TagViewModel>中。 DataGrid公开了我尝试在TagViewModel中显示的一些属性。

我创建了一个按钮,将一个项目添加到DataGrid,并显示一个消息框,其中包含集合的项目计数和数据网格的项目计数。该集合正在获取所有项目,但ObservableCollection<TagViewModel>始终为0计数。

以下是带有数据绑定的XAML代码:

DataGrid

以下是TagGridView的代码隐藏文件:

<UserControl x:Class="Company.TagVisualizer.Views.TagGridView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" Width="Auto" Height="Auto" Name="TagGrid" DataContext="{Binding Source=TagGridContext}">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <DataGrid Name="DataGridTag" Grid.Column="0" Grid.Row="0" ItemsSource="{Binding Path=TagsCollection}" Margin="0" CanUserReorderColumns="False" CanUserResizeColumns="True" CanUserAddRows="True" CanUserDeleteRows="True">
            <DataGrid.Columns>
                <DataGridCheckBoxColumn Width="*"  Header="Selected" />
                <DataGridTextColumn Width="3*" Binding="{Binding Path=TagName}" Header="Tag Name"/>
                <DataGridTextColumn Width="3*" Binding="{Binding Detections.Count}" Header="Detections"/>
                <DataGridTextColumn Width="3*" Binding="{Binding EarliestDetection}"  Header="Earliest Detection"/>
                <DataGridTextColumn Width="3*" Binding="{Binding LatestDetection}" Header="Latest Detection"/>
            </DataGrid.Columns>
        </DataGrid>
        <Button Grid.Column="0" Grid.Row="1" Content="Add" Click="Add_OnClick"/>
    </Grid>

</UserControl>  

以下是public partial class TagGridView : UserControl { private TagGridViewModel _tagGridViewModel = new TagGridViewModel(); public TagGridView() { InitializeComponent(); } public TagGridViewModel TagGridContext { get { return _tagGridViewModel; } set { _tagGridViewModel = value; } } private void Add_OnClick(object sender, RoutedEventArgs e) { TagGridContext.TagsCollection.Add(new TagViewModel("Boogie", 134.5, 145.8)); MessageBox.Show(string.Format("Items in collection: {0}\nItems in Grid: {1}", TagGridContext.TagsCollection.Count, DataGridTag.Items.Count)); } } 类:

TagGridViewModel

我不确定发生了什么。我尝试在public class TagGridViewModel { private ObservableCollection<TagViewModel> _tagsCollection = new ObservableCollection<TagViewModel>(); public ObservableCollection<TagViewModel> TagsCollection { get { return _tagsCollection; } set { _tagsCollection = value; } } } 标记和UserControl标记中设置DataContext。无论我做什么,DataGrid都不会向其自身添加任何项目。

1 个答案:

答案 0 :(得分:1)

是usercontrol Datacontext问题。请参考以下代码进行数据绑定。

<UserControl x:Class="Tags_Learning.TagGridView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300" **DataContext="{Binding Path=TagGridContext, RelativeSource={RelativeSource Self}}"**>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <DataGrid Name="DataGridTag" Grid.Column="0" Grid.Row="0" ItemsSource="{Binding Path=TagsCollection}" Margin="0" 
              CanUserReorderColumns="False" CanUserResizeColumns="True" CanUserAddRows="True" CanUserDeleteRows="True">
        <DataGrid.Columns>
            <DataGridCheckBoxColumn Width="*"  Header="Selected" />
            <DataGridTextColumn Width="3*" Binding="{Binding Path=TagName}" Header="Tag Name"/>                
            <DataGridTextColumn Width="3*" Binding="{Binding EarliestDetection}"  Header="Earliest Detection"/>
            <DataGridTextColumn Width="3*" Binding="{Binding LatestDetection}" Header="Latest Detection"/>
        </DataGrid.Columns>
    </DataGrid>
    <Button Grid.Column="0" Grid.Row="1" Content="Add" Click="Add_OnClick"/>
</Grid>