2013-10-29 94 views
0

我在我的主窗口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> 

+3

1.我錯過了網格的觀點,我認爲......你應該有行或列(或兩者),並且第二個控件在另一個控件中。 2.該列表框中要添加什麼,從哪個按鈕? – Noctis

+3

我在這裏沒有看到任何問題。你應該問一個與你有這個問題有關的問題。我們不會只爲你寫代碼。 – nvoigt

+0

@ user2889489好,那麼,你嘗試過什麼,什麼是你對這些方法的代碼? ('ButtonCommand'和'SendControlCommand')。 – Noctis

回答

2

聲音對我說,你在黑暗中刺傷...... 你說你正在使用MVVM和WPF,但我認爲你應該修改這些主題的第一你的背景...

基本上,你的視圖應該綁定到ViewModel上的屬性。

通常你會希望有一個可觀察的集合,將列表框的源,而在你的XAML這樣做

<ListBox Name="listbox_name" ... ItemSource="{Binding ListPropertyName}/> 

(我假設你有一個類型的屬性的ObservableCollection命名ListPropertyName,顯然你會根據自己的需要將其命名爲別的

然後,命令一旦你:。

<Button Command="{Binding ButtonCommand}" Name="btn1" Content="Send"... /> 

這意味着你需要有一個ICommand屬性在您的視圖模型的代碼,稱爲ButtonCommand

public ICommand ButtonCommand{ get; private set; } 

在你的構造器,那麼你可以這樣寫:

ButtonCommand= new RelayCommand<object>(Execute_YourMethodHere); 

現在,當你點擊按鈕,您的運行Execute_YourMethodHere

在這裏,你可能會想的對象添加到您的ObservableCollection(假設你已經使用INotifyPropertyChanged的,所以查看就知道了您的收藏改變),這的的確確是它...

0

你忘了DataContext? 在代碼後面,你應該加上這一行: this.DataContext = new YourViewModel();

你能顯示你的CommandMethods嗎?

+0

在ViewModel類pls中顯示你的代碼? – user2830464

0

你忘了綁定的的ItemsSource的名單在第二XAML。如果你想這樣做與MVVM正確的方式,你需要兩個的ItemsSource屬性,二relaycommands並在視圖模型中的兩個字符串屬性。在命令行爲中,只需將適當的文本添加到合適列表的ItemsSource中即可。

你還需要知道INotifyPropertyChanged的,和的ObservableCollection的ICommand,使其工作。