我已经使用VS2010使用.Net 4.0创建了动态控件。当我点击一个按钮时,我用来创建不同的控件取决于xml文件的输入,并使用" Content"将动态创建的控件设置为WPF窗口。控制。源代码如下,
ButtonClickMethod.cs
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>
我们正在更新框架并使用Catel MVVM框架。现在我想转换相同的功能来支持.Net 4.6.2
和MVVM。
在更新版本中,我计划将创建的控件绑定在ScrollerViewer中。
<catel:DataWindow
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:Labeling.Views"
xmlns:catel="http://schemas.catelproject.com"
xmlns:orc="http://www.wildgums.net.au/orc" x:Class="Labeling.Views.CustomFieldListView"
mc:Ignorable="d" Icon="{StaticResource Icons8Printer}"
Title="FieldsConfiguration" Height="270" Width="584" WindowStartupLocation="CenterScreen" SizeToContent="Manual" ResizeMode="NoResize">
<Grid>
<ScrollViewer Grid.ColumnSpan="2" Margin="0,51.424,10,16.251" Grid.Row="1">
</ScrollViewer>
</Grid>
</catel:DataWindow>
上述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
是将创建的控件设置为内容的响应。在更新的框架中,我使用的是MVVM,我想知道如何在上面的View加载时将创建的控件设置为ScrollViewer。
请帮我这样做。
谢谢你