如何在RadGridView的一行中填充数据?

时间:2014-10-03 09:05:44

标签: c# wpf telerik-grid

我想从网格中保存和加载设置。保存设置时,此设置将保存在文件中。当我加载设置时,将再次放入网格中。只有数据不会显示在网格中。是什么原因?

xaml.cs:

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        string fileName = @"C:\temp\myFile.txt";
        private Stream stream;

        public MainWindow()
        {
            InitializeComponent();
            InitializeSettings();
            InitializeDataContext();
        }

        private void InitializeSettings()
        {
            using (var filestream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Read))
            {
                PersistenceManager manager = new PersistenceManager();
                manager.Load(filestream);
            }
        }

        private void InitializeDataContext()
        {
            DataContext = new ViewModel();
        }

        private void Window_Closing(object sender, CancelEventArgs e)
        {
            PersistenceManager manager = new PersistenceManager();
            stream = manager.Save(myGrid);
            using (var fileStream = new FileStream(fileName, FileMode.Create, FileAccess.Write))
            {
                stream.CopyTo(fileStream);
            }
        }
    }
}

xaml文件:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="MainWindow"
        Height="350"
        Width="525"
        Closing="Window_Closing">
    <StackPanel x:Name="LayoutRoot">
        <telerik:RadGridView x:Name="myGrid"
                             IsReadOnly="False"
                             ItemsSource="{Binding Path=Afspraken, Mode=OneWay}"
                             AddingNewDataItem="myGrid_AddingNewDataItem"
                             RowEditEnded="myGrid_RowEditEnded"
                             AutoGenerateColumns="False"
                             CanUserSortColumns="True"
                             CanUserResizeColumns="True"
                             telerik:PersistenceManager.StorageId="myGrid">
            <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn UniqueName="Afspraak"
                                            Header="Afspraak"
                                            DataMemberBinding="{Binding Path=Omschrijving, Mode=OneWay}"/>
                <telerik:GridViewDataColumn UniqueName="Taak"
                                            Header="Taak"
                                            DataMemberBinding="{Binding Path=Opdracht, Mode=OneWay}"/>
                <telerik:GridViewDataColumn UniqueName="UniqueIdea"
                                            Header="Unique Idea"
                                            DataMemberBinding="{Binding Path=PrimaryId, Mode=OneWay}"/>
            </telerik:RadGridView.Columns>
        </telerik:RadGridView>
    </StackPanel>
</Window>

视图模型:

    public class ViewModel : ViewModelBase
    {
        public ViewModel()
        {
            ReadData();
        }

        public void ReadData()
        {
            afspraken.Add(new Afspraak() { Omschrijving = "Afspraak 1", Opdracht = "Opdracht 1", PrimaryId = 1 });
            afspraken.Add(new Afspraak() { Omschrijving = "Afspraak 2", Opdracht = "Opdracht 2", PrimaryId = 2 });
        }

        private ObservableCollection<Afspraak> afspraken = new ObservableCollection<Afspraak>();
        public ObservableCollection<Afspraak> Afspraken
        {
            get { return afspraken; }
            set
            {
                if (afspraken != value)
                {
                    afspraken = value;
                    OnPropertyChanged("Afspraken");
                }
            }
        }
   }

1 个答案:

答案 0 :(得分:0)

您没有告诉PersistenceManager要加载哪个控件,因为您没有将myGrid作为PersistenceManager.Load()方法的参数传递:

using (var filestream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Read))
{
    PersistenceManager manager = new PersistenceManager();
    manager.Load(filestream);
}

对于TreeView

,从Telerik documentation看起来应如下所示
private void Load(object sender, RoutedEventArgs e)
{
    stream.Position = 0L;
    PersistenceManager manager = new PersistenceManager();
    manager.Load(treeView, stream);
}

我认为您需要根据您的情况进行调整。