简体   繁体   English

WPF MVVM 模型中的按钮命令

[英]Button Command in WPF MVVM Model

I have two UserControls in my MainWindow and UserControl2 has 2 Listboxes,Texboxes and Buttons.When i write some text in TextBox and press Button it should add into the ListBox.Can somebody help me with the code,i'm new to WPF and MVVM我的 MainWindow 中有两个 UserControls,UserControl2 有 2 个 Listboxes、Texboxes 和 Buttons。当我在 TextBox 中写入一些文本并按下 Button 时,它应该添加到 ListBox 中。有人可以帮我编写代码吗,我是 WPF 和 MVVM 的新手

This is my XAML code这是我的 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>

This is my UserControl1.Xaml code这是我的 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>

This is my UserControl2.Xaml code这是我的 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>

Sounds to me that you're stabbing in the dark ... You say you're using MVVM and WPF, but I think you should revise your background on those topics first ...听起来你是在黑暗中刺伤......你说你正在使用 MVVM 和 WPF,但我认为你应该首先修改你对这些主题的背景......

Basically, your View should bind to properties on the ViewModel.基本上,您的视图应该绑定到 ViewModel 上的属性。

Usually you'll want to have an observable collection that would be the source for the list box, and the on your XAML do something like通常你会想要一个可观察的集合作为列表框的来源,并且在你的 XAML 上做一些类似的事情

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

(I'm assuming you have a property of type ObservableCollection named ListPropertyName , obviously you'll name it something else according to your needs) (我假设您有一个名为ListPropertyName ObservableCollection类型的属性,显然您会根据需要将其命名为其他名称)

Then, the commands.然后,命令。 Once you have:一旦你有:

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

It means you need to have an ICommand property in your view models code, called ButtonCommand :这意味着您需要在视图模型代码中有一个 ICommand 属性,称为ButtonCommand

public ICommand ButtonCommand{ get; private set; }

In your contructor you could then write:在你的构造函数中,你可以这样写:

ButtonCommand= new RelayCommand<object>(Execute_YourMethodHere);

Now, when you hit the button, your Execute_YourMethodHere is run.现在,当您点击按钮时,您的Execute_YourMethodHere就会运行。

This is where you'll probably want to add the object to your ObservableCollection (assuming you have used INotifyPropertyChanged, so the View will know your collection changed), and that's really about it ...这是您可能希望将对象添加到 ObservableCollection 的地方(假设您使用了 INotifyPropertyChanged,因此视图将知道您的集合已更改),这就是真正的问题......

Did you forget about DataContext ?你忘记了DataContext吗? In behind code, you should add this line: this.DataContext = new YourViewModel();在后面的代码中,您应该添加这一行: this.DataContext = new YourViewModel();

Can you show your CommandMethods?你能展示你的命令方法吗?

You forget the bind an ItemsSource to the lists in the second xaml.您忘记了将 ItemsSource 绑定到第二个 xaml 中的列表。 If you want to do it in a right way with MVVM, you need two ItemsSource properties, two relaycommands and two string properties in the view model.如果你想用 MVVM 以正确的方式做到这一点,你需要视图模型中的两个 ItemsSource 属性、两个中继命令和两个字符串属性。 In the command action simply add the appropriate text to the appropiate list's ItemsSource.在命令操作中,只需将适当的文本添加到适当列表的 ItemsSource。

You also need to aware of INotifyPropertyChanged, ObservableCollection and ICommand to make it work.您还需要了解 INotifyPropertyChanged、ObservableCollection 和 ICommand 以使其工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM