我在我的主窗口2個用戶控件和UserControl2有2個列表框,Texboxes和Buttons.When我寫一些文字在文本框和按鍵應該加入到ListBox.Can有人幫助我的代碼,我是新來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>
1.我錯過了網格的觀點,我認爲......你應該有行或列(或兩者),並且第二個控件在另一個控件中。 2.該列表框中要添加什麼,從哪個按鈕? – Noctis
我在這裏沒有看到任何問題。你應該問一個與你有這個問題有關的問題。我們不會只爲你寫代碼。 – nvoigt
@ user2889489好,那麼,你嘗試過什麼,什麼是你對這些方法的代碼? ('ButtonCommand'和'SendControlCommand')。 – Noctis