C# 在ScrollViewer-WPF中绑定动态创建的控件
我已经使用VS2010使用.Net 4.0创建了动态控件。当我单击一个按钮时,我常常根据xml文件的输入创建不同的控件,并使用“内容”控件将动态创建的控件设置为WPF窗口。下面是源代码 按钮ClickMethod.csC# 在ScrollViewer-WPF中绑定动态创建的控件,c#,wpf,mvvm,data-binding,catel,C#,Wpf,Mvvm,Data Binding,Catel,我已经使用VS2010使用.Net 4.0创建了动态控件。当我单击一个按钮时,我常常根据xml文件的输入创建不同的控件,并使用“内容”控件将动态创建的控件设置为WPF窗口。下面是源代码 按钮ClickMethod.cs private static void CustomControlsButton_Click(object source, RoutedEventArgs args) //button click event { CustomControls cui
private static void CustomControlsButton_Click(object source, RoutedEventArgs args) //button click event
{
CustomControls cui = new CustomControls();
if (isFileExist)
{
cui.Content = cui.CreateGrid(); //Code which set the created controls to the content
cui.ShowDialog();
}
}
CreateCustomControls.cs
public object CreateGrid() // responsible for control creation
{
int noOfRows = Attributes.Count;
new PopulateGrid(noOfRows, Attributes);
return grid.Get();
}
CustomControls.xaml
<Window x:Class="Client.CustomControls"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:resx="clr-namespace:YottaMark.Wpf.Label.Client.Properties"
Title="Custom Controls" Height="300" Width="300">
<Grid>
</Grid>
</Window>
上述WPF的ViewModel.cs
public class CustomFieldListViewModel : ViewModelBase
{
private readonly IAttributesService _AttributesService;
public bool AttributesFileExists { get; set;}
public bool AttributesExists { get; set; }
private static JArray Attributes;
private static DynamicGrid grid = new DynamicGrid();
public CustomControlListViewModel(IAttributesService AttributesService)
{
_AttributesService = AttributesService;
AttributesFileExists = false;
AttributesExists = false;
}
}
行cui.Content=cui.CreateGrid()ButtonClickMethod.cs
中的code>是将创建的控件设置为内容的响应。在更新的框架中,我正在使用MVVM,我想知道如何在加载上述视图时将创建的控件设置为ScrollViewer
请帮我做这件事
谢谢您您应该使用数据模板和绑定创建控件。是否有要在scrollviewer中显示的viewmodel?@EdPlunkett是的,我有要显示的viewmodel。你能给我一个示例吗?我不能给你一个示例代码,说明如何处理具有未知属性的未知viewmodel。viewmodel代表什么?它是否实现INotifyPropertyChanged?它是什么的数据上下文吗?什么,在哪里?@EdPlunkett我在问题中更新了ViewModel类。请复习
public class CustomFieldListViewModel : ViewModelBase
{
private readonly IAttributesService _AttributesService;
public bool AttributesFileExists { get; set;}
public bool AttributesExists { get; set; }
private static JArray Attributes;
private static DynamicGrid grid = new DynamicGrid();
public CustomControlListViewModel(IAttributesService AttributesService)
{
_AttributesService = AttributesService;
AttributesFileExists = false;
AttributesExists = false;
}
}