从另一个 UserControl 创建 UserControl 并在运行时添加到父容器
Create UserControl from another UserControl and add to parent container in runtime
我有一个简单的 UserControl,其中包含一个 TextBox 和一个 Button。
我希望单击该按钮将创建一个新的用户控件,该控件将位于单击其按钮的控件之后(下方)。
例如:
这是用户控件的代码:
<UserControl x:Class="LearnWPF.MyUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height=".9*"/>
<RowDefinition Height=".1*"/>
</Grid.RowDefinitions>
<TextBox ></TextBox>
<Button Grid.Row="1" Content="Create New UserControl" FontSize="20"/>
</Grid>
这是主要的window:
<Window x:Class="LearnWPF.MyWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:myUserControl="clr-namespace:LearnWPF"
Title="MainWindow" Height="450" Width="800">
<StackPanel>
<myUserControl:MyUserControl />
</StackPanel>
</Window>
我确实按照建议 here 成功地将新 "myUserControls" 添加到主 window 中的 StackPanel。此实现的 2 个主要问题是:
- 我无法确定我是从哪个 "myUserControl" 得到事件的。
- 即使我确实知道单击了哪个按钮以及在何处创建新的 UserControl,我也不知道如何在 StackPanel 的中间插入(也许是
需要不同的面板?)
此外,此解决方案使用了隐藏代码。有没有办法在 MVVM 中完成所有这些工作?
如果您想从视图模型管理项目列表,则需要使用 ItemsControl
而不是从代码隐藏向 StackPanel 插入元素。可以通过 ItemsControl.ItemTemplate
更改项目外观。添加新项目的操作应由绑定到按钮的命令触发:
<Window x:Class="XamlApp.MyWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MyWindow" WindowStartupLocation="CenterScreen"
Height="300" Width="300">
<Grid>
<ScrollViewer>
<ItemsControl ItemsSource="{Binding Path=MyItems}"
Margin="5"
Background="Wheat">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height=".9*" />
<RowDefinition Height=".1*" />
</Grid.RowDefinitions>
<TextBox Text="{Binding Path=MyText}" />
<Button Grid.Row="1"
Content="Create New UserControl"
FontSize="20"
Command="{Binding Path=DataContext.AddCmd,
RelativeSource={RelativeSource AncestorType=ItemsControl}}"
CommandParameter="{Binding}"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</Grid>
</Window>
视图模型应该有一个存储项目的集合和一个到 add/insert 个项目的命令:
public class StringItem
{
public string MyText { get; set; }
}
public class MyViewModel
{
public MyViewModel()
{
MyItems = new ObservableCollection<StringItem>();
AddCmd = new RelayCommand<StringItem>(Add);
}
public ObservableCollection<StringItem> MyItems { get; private set; }
public ICommand AddCmd { get; private set; }
private void Add(StringItem current)
{
var item = new StringItem { MyText = "new item " + (MyItems.Count + 1) };
int idx = MyItems.IndexOf(current);
if (idx < 0)
MyItems.Add(item);
else
MyItems.Insert(idx + 1, item);
}
}
RelayCommand 是 this lightweight MVVM package
的 ICommand 的实现
ViewModel 和 View 在 View 构造函数中连接在一起:
public partial class MyWindow : Window
{
public MyWindow()
{
InitializeComponent();
DataContext = new MyViewModel
{
MyItems = { new StringItem { MyText = "hello world" } }
};
}
}
我有一个简单的 UserControl,其中包含一个 TextBox 和一个 Button。 我希望单击该按钮将创建一个新的用户控件,该控件将位于单击其按钮的控件之后(下方)。
例如:
这是用户控件的代码:
<UserControl x:Class="LearnWPF.MyUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height=".9*"/>
<RowDefinition Height=".1*"/>
</Grid.RowDefinitions>
<TextBox ></TextBox>
<Button Grid.Row="1" Content="Create New UserControl" FontSize="20"/>
</Grid>
这是主要的window:
<Window x:Class="LearnWPF.MyWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:myUserControl="clr-namespace:LearnWPF"
Title="MainWindow" Height="450" Width="800">
<StackPanel>
<myUserControl:MyUserControl />
</StackPanel>
</Window>
我确实按照建议 here 成功地将新 "myUserControls" 添加到主 window 中的 StackPanel。此实现的 2 个主要问题是:
- 我无法确定我是从哪个 "myUserControl" 得到事件的。
- 即使我确实知道单击了哪个按钮以及在何处创建新的 UserControl,我也不知道如何在 StackPanel 的中间插入(也许是 需要不同的面板?)
此外,此解决方案使用了隐藏代码。有没有办法在 MVVM 中完成所有这些工作?
如果您想从视图模型管理项目列表,则需要使用 ItemsControl
而不是从代码隐藏向 StackPanel 插入元素。可以通过 ItemsControl.ItemTemplate
更改项目外观。添加新项目的操作应由绑定到按钮的命令触发:
<Window x:Class="XamlApp.MyWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MyWindow" WindowStartupLocation="CenterScreen"
Height="300" Width="300">
<Grid>
<ScrollViewer>
<ItemsControl ItemsSource="{Binding Path=MyItems}"
Margin="5"
Background="Wheat">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height=".9*" />
<RowDefinition Height=".1*" />
</Grid.RowDefinitions>
<TextBox Text="{Binding Path=MyText}" />
<Button Grid.Row="1"
Content="Create New UserControl"
FontSize="20"
Command="{Binding Path=DataContext.AddCmd,
RelativeSource={RelativeSource AncestorType=ItemsControl}}"
CommandParameter="{Binding}"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</Grid>
</Window>
视图模型应该有一个存储项目的集合和一个到 add/insert 个项目的命令:
public class StringItem
{
public string MyText { get; set; }
}
public class MyViewModel
{
public MyViewModel()
{
MyItems = new ObservableCollection<StringItem>();
AddCmd = new RelayCommand<StringItem>(Add);
}
public ObservableCollection<StringItem> MyItems { get; private set; }
public ICommand AddCmd { get; private set; }
private void Add(StringItem current)
{
var item = new StringItem { MyText = "new item " + (MyItems.Count + 1) };
int idx = MyItems.IndexOf(current);
if (idx < 0)
MyItems.Add(item);
else
MyItems.Insert(idx + 1, item);
}
}
RelayCommand 是 this lightweight MVVM package
的 ICommand 的实现ViewModel 和 View 在 View 构造函数中连接在一起:
public partial class MyWindow : Window
{
public MyWindow()
{
InitializeComponent();
DataContext = new MyViewModel
{
MyItems = { new StringItem { MyText = "hello world" } }
};
}
}