2017-06-01 103 views
2

问题:DataGrid显示为空,但是我有相关信息,并且我的DataGrid收到信息但仍为空!DataGrid显示为空MVVM Wpf

我的XAML:

<DataGrid x:Name="dataGrid" Grid.Row="1" ItemsSource="{Binding ViewList}" 
      CanUserAddRows="False" AlternationCount="2" 
      AlternatingRowBackground="Blue"> 
    <DataGrid.Columns> 
    <DataGridTextColumn Header="View" Binding="{Binding id}" 
         Width="2*" IsReadOnly="True" /> 
    <DataGridTemplateColumn Header="Is Enabled" Width="Auto"> 
     <DataGridTemplateColumn.CellTemplate> 
     <DataTemplate> 
      <CheckBox IsChecked="{Binding isEnabled, Mode=TwoWay, 
         UpdateSourceTrigger=PropertyChanged}"/> 
     </DataTemplate> 
     </DataGridTemplateColumn.CellTemplate> 
    </DataGridTemplateColumn>     
    </DataGrid.Columns> 
</DataGrid> 

我的视图模型:

public ConfigRoleModel() 
{ 
    _viewList = new ObservableCollection<ViewList>(WCFclient.getListOfViews(1)); 
} 

private ObservableCollection<ViewList> _viewList; 
public ObservableCollection<ViewList> ViewList 
{ 
    get { return _viewList; } 
    set 
    { 
     _viewList = value; 
     OnPropertyChanged("ViewList"); 
    } 
} 

Viewlist产品类:

public class ViewList 
{ 
    public int id;  
    public string description; 
    public string code; 
} 

这是结果:enter image description here

我怎样才能解决呢?

+2

请添加ViewList类 –

+0

至少对于'isEnabled'来说这是一个简单的情况...... ViewList中没有这样的属性。 'id'有些不同。如果你需要帮助,我想你应该真的创建一个[mcve](https://stackoverflow.com/help/mcve)。 – grek40

+0

在您的ViewList类中为id和公共属性定义... – tabby

回答

1

通过看你的代码:

  1. 你应该在你Viewlist产品类中定义的公共财产的结合工作。

  2. 将Datacontext设置为您的viewModel。

  3. 在你的DataContext
  4. 没有IsEnabled属性

你Viewlist产品类别应该是这样的:

public class ViewList 
{ 
    public int Id { get; set; } 
    public bool IsEnabled { get; set; } 
    ... 
} 

和XAML中:

<DataGrid x:Name="dataGrid" Grid.Row="1" ItemsSource="{Binding ViewList}" 
      CanUserAddRows="False" AlternationCount="2" AlternatingRowBackground="Blue" > 
    <DataGrid.Columns> 
     <DataGridTextColumn Header="View" Binding="{Binding Id}" Width="2*" IsReadOnly="True" /> 

     <DataGridTemplateColumn Header="Is Enabled" Width="Auto"> 
      <DataGridTemplateColumn.CellTemplate> 
       <DataTemplate> 
        <CheckBox IsChecked="{Binding IsEnabled , Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/> 
       </DataTemplate> 
      </DataGridTemplateColumn.CellTemplate> 
     </DataGridTemplateColumn>     
    </DataGrid.Columns> 
</DataGrid> 

而且在你看来后面的代码或者在你的xaml本身:

  • 设置你的DataContext你的视图模型
+0

非常非常感谢, – devtunis

0

字段不为WPF绑定有效的目标。您应该改用属性。

public class ViewList { 
    public int Id { get; set; } 
    public string Description { get; set; } 
    public string Code { get; set; } 
    public bool IsEnabled { get; set; } 
} 
+0

非常感谢! – devtunis

0

请确保您的视图列表类实现INotifyPropertyChanged

public class ViewList : INotifyPropertyChanged 
{ 
    private int _id; 
    public int id 
    { 
     get { return _id; } 
     set 
     { 
      _id = value; 
      OnPropertyChanged(new PropertyChangedEventArgs("id")); 
     } 
    } 

    private string _description; 
    public string description 
    { 
     get { return _description; } 
     set 
     { 
      if((value as string) != null) 
      { 
       _description = value; 
       OnPropertyChanged(new PropertyChangedEventArgs("description")); 
      } 
     } 
    } 

    private string _code; 
    public string code 
    { 
     get { return _code; } 
     set 
     { 
      _code = value; 
      OnPropertyChanged(new PropertyChangedEventArgs("code")); 
     } 
    } 

    private bool _isEnabled; 
    public bool isEnabled 
    { 
     get { return _isEnabled; } 
     set 
     { 
      _isEnabled = value; 
      OnPropertyChanged(new PropertyChangedEventArgs("isEnabled")); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    public void OnPropertyChanged(PropertyChangedEventArgs e) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, e); 
     } 
    } 
} 

您的视图模型并不需要执行INotifyPropertyChanged如果你只是想从的ObservableCollection显示数据。这是我的ViewModel:

public class MainWindowVM 
{ 
    private ObservableCollection<ViewList> _MyList; 
    public ObservableCollection<ViewList> MyList 
    { 
     get { return _MyList; } 
     set 
     { 
      if(value != null) 
      { 
       _MyList = value; 
      } 
     } 
    } 

    public MainWindowVM() 
    { 
     _MyList = new ObservableCollection<WpfDataGridTest.ViewList>(); 

     _MyList.Add(new WpfDataGridTest.ViewList() { id = 1, code = "C101", description = "test1", isEnabled = true }); 
     _MyList.Add(new WpfDataGridTest.ViewList() { id = 2, code = "C102", description = "test2", isEnabled = false }); 
     _MyList.Add(new WpfDataGridTest.ViewList() { id = 3, code = "C103", description = "test3", isEnabled = true }); 
    } 
} 

这里是我的窗口的XAML

<Window x:Class="WpfDataGridTest.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:WpfDataGridTest" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="350" Width="525" 
     DataContext="{StaticResource MainWindowVM}"> 
    <Grid> 
     <DataGrid x:Name="dataGrid" HorizontalAlignment="Left" Margin="33,34,0,0" VerticalAlignment="Top" Height="236" Width="444" 
        CanUserAddRows="False" AlternationCount="2" AlternatingRowBackground="Blue" AutoGenerateColumns="False" IsSynchronizedWithCurrentItem="True" 
        ItemsSource="{Binding MyList}"> 
      <DataGrid.Columns> 
       <DataGridTextColumn Header="View" Binding="{Binding id}" Width="2*" IsReadOnly="True" /> 

       <DataGridTemplateColumn Header="Is Enabled" Width="Auto"> 
        <DataGridTemplateColumn.CellTemplate> 
         <DataTemplate> 
          <CheckBox IsChecked="{Binding isEnabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/> 
         </DataTemplate> 
        </DataGridTemplateColumn.CellTemplate> 
       </DataGridTemplateColumn> 
      </DataGrid.Columns> 
     </DataGrid> 
    </Grid> 

    <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="44,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" 
      Text="{Binding Path=CurrentItem.description, ElementName=dataGrid}"/> 
</Window> 

这个例子显示了3行如预期我使用VS 2015,你可以在这里看到:

WPFDataGridTest app capture 注意:我将ViewModel的ViewList成员重命名为MyList,因为我不喜欢让成员与类相同的名称,因为它可能会使事情变得混乱。