我在MainWindow中有两个UserControls,而UserControl2有2个Listboxes,Texboxes和Buttons。当我在TextBox中写一些文本并按Button时,它应该添加到ListBox中。有人可以帮助我提供代码,我是WPF的新手。和MVVM
这是我的XAML代码
<Window x:Class="Wpf_MVVM.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Wpf_MVVM"
Title="Voxer" Background="SlateGray" Height="420" Width="550">
<Grid>
<local:UserControl1 HorizontalAlignment="Left" VerticalAlignment="Top"/>
<local:UserControl2 HorizontalAlignment="Left" VerticalAlignment="Top" Margin="150,29,0,0"/>
</Grid>
这是我的UserControl1.Xaml代码
<UserControl x:Class="Wpf_MVVM.UserControl1"
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"
Height="Auto" Width="Auto">
<Grid>
<ListBox HorizontalAlignment="Left" Height="310" VerticalAlignment="Top" Width="150" Margin="0,40,0,0">
<ListBoxItem>Name 1</ListBoxItem>
<ListBoxItem>Name 2</ListBoxItem>
<ListBoxItem>Name 3</ListBoxItem>
<ListBoxItem>Name 4</ListBoxItem>
<ListBoxItem>Name 5</ListBoxItem>
<ListBoxItem>Name 6</ListBoxItem>
</ListBox>
<Label Content="Conversations" HorizontalAlignment="Left" VerticalAlignment="Top" Height="40" Width="150" FontSize="20" Background="SkyBlue"/>
<Button Content="Create New Chat" Height="30" HorizontalAlignment="Left" Margin="0,350,0,0" VerticalAlignment="Top" Width="150"/>
</Grid>
这是我的UserControl2.Xaml代码
<UserControl x:Class="Wpf_MVVM.UserControl2"
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"
Height="Auto" Width="390">
<Grid>
<ListBox Name="listbox1" HorizontalAlignment="Left" Height="310" VerticalAlignment="Top" Width="180" Margin="10,0,0,0"/>
<ListBox Name="listbox2" HorizontalAlignment="Left" Height="310" Margin="200,0,0,0" VerticalAlignment="Top" Width="180"/>
<TextBox Name="tb1" HorizontalAlignment="Left" Height="40" Margin="200,310,0,0" TextWrapping="NoWrap" Text="" VerticalAlignment="Top" Width="130"/>
<TextBox Name="tb2" Height="40" TextWrapping="NoWrap" Text="" Margin="10,310,245,0"/>
<Button Command="{Binding ButtonCommand}" Name="btn1" Content="Send" Height="40" HorizontalAlignment="Left" Margin="330,310,0,0" VerticalAlignment="Top" Width="50" />
<Button Command="{Binding SendControlCommand}" Name="btn2" Content="Send" Height="40" Margin="145,310,200,0"/>
</Grid>
基本上,您的View应该绑定到ViewModel的属性。
通常,您希望有一个可观察的集合,它将成为列表框的源,并且在XAML上执行类似的操作
<ListBox Name="listbox_name" ... ItemSource="{Binding ListPropertyName}/>
(我假设您有一个名为ObservableCollection
的ListPropertyName
类型的属性,显然您会根据需要将其命名为其他名称)然后是命令。一旦拥有:
<Button Command="{Binding ButtonCommand}" Name="btn1" Content="Send"... />
这意味着您需要在视图模型代码中拥有一个称为ButtonCommand
的ICommand属性:
public ICommand ButtonCommand{ get; private set; }
然后您可以在建设者中编写:
ButtonCommand= new RelayCommand<object>(Execute_YourMethodHere);
现在,当您按下按钮时,您的Execute_YourMethodHere
运行。这可能是您想将对象添加到ObservableCollection的地方(假设您已经使用了INotifyPropertyChanged,因此View会知道您的集合已更改),而实际上就是它了...
DataContext
?在后面的代码中,您应该添加以下行:this.DataContext = new YourViewModel();
您可以显示您的CommandMethods吗?