0
當前我直接從我的xaml.cs文件加載數據,但我現在想從XML文件加載數據,因爲我目前沒有關於如何執行的知識這個我在想,如果人可以幫助我,這是我當前的文件:從XML(WPF)加載數據
enter code here
//Create a viewmodel and add some data to it.
var viewModel = new MyViewModel();
viewModel.Items.Add(new Data() { Name = "Yes", Type = "Yes", Selected = true });
viewModel.Items.Add(new Data() { Name = "Yes", Type = "No", Selected = true });
viewModel.Items.Add(new Data() { Name = "No", Type = "No", Selected = true });
viewModel.Items.Add(new Data() { Name = "Yes", Type = "No", Selected = true });
viewModel.Items.Add(new Data() { Name = "No", Type = "No", Selected = true });
viewModel.Items.Add(new Data() { Name = "No", Type = "No", Selected = true });
viewModel.Items.Add(new Data() { Name = "No", Type = "No", Selected = true });
viewModel.Items.Add(new Data() { Name = "No", Type = "No", Selected = true });
viewModel.Items.Add(new Data() { Name = "No", Type = "No", Selected = true });
viewModel.Items.Add(new Data() { Name = "Unknown", Type = "No", Selected = true });
viewModel.Items.Add(new Data() { Name = "Yes", Type = "No", Selected = true });
viewModel.Items.Add(new Data() { Name = "Yes", Type = "No", Selected = true });
viewModel.Items.Add(new Data() { Name = "No", Type = "No", Selected = true });
viewModel.Items.Add(new Data() { Name = "Yes", Type = "No", Selected = true });
viewModel.Items.Add(new Data() { Name = "No", Type = "No", Selected = true });
viewModel.Items.Add(new Data() { Name = "No", Type = "No", Selected = true });
//Set the window's datacontext to the ViewModel. This will make binding work.
this.DataContext = viewModel;
}
}
//This is the ViewModel used to bind data
public class MyViewModel
{
//This could just be a List<Data> but ObservableCollection<T> will automatically
//update UI when items are added or removed from the collection.
public ObservableCollection<Data> Items { get; set; }
public MyViewModel()
{
Items = new ObservableCollection<Data>();
}
}
//Just a sample class to hold the data for the grid.
//This is the class that is contained in the ObservableColleciton in the ViewModel
public class Data
{
public string Name { get; set; }
public string Type { get; set; }
public bool Selected { get; set; }
}
//This is an example converter. It looks to see if the element is set to "Yes"
//If so, it returns Visibility.Collapsed. Otherwise, it returns Visibility.Visible.
public class YesToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value != null && value is string)
{
var input = (string)value;
if (string.Equals(input, "Yes", StringComparison.CurrentCultureIgnoreCase))
{
return Visibility.Collapsed;
}
else
{
return Visibility.Visible;
}
}
return Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
請在搜索Google/StackOverflow之前發佈像這些常見問題... http://stackoverflow.com/questions/8972743/load-xmldataprovider-in-code –
我會先看看MSDN,並檢查出主題與XML,XLinq,XML序列化相關。 HTH。 – code4life