【问题标题】:Button Command in WPF MVVM ModelWPF MVVM 模型中的按钮命令
【发布时间】:2013-10-29 06:34:41
【问题描述】:

我的 MainWindow 中有两个 UserControl,而 UserControl2 有 2 个 Listbox、Texbox 和 Button。当我在 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>

【问题讨论】:

  • 1.你错过了我认为的网格点......你应该有行或列(或两者),并在另一个控件中有第二个控件。 2.你想从哪个按钮添加什么到哪个列表框?
  • 我在这里没有看到任何问题。你应该问一个与你遇到的问题有关的问题。我们不会只为您编写代码。
  • @user2889489 好的,那么,您尝试了什么,这些方法的代码是什么? (ButtonCommand & SendControlCommand)。
  • @user2889489 你是什么意思它没有工作?什么没用。当您按下按钮时,您的方法是否真正被调用?您如何绑定到列表的来源?

标签: c# wpf xaml mvvm listbox


【解决方案1】:

听起来你在黑暗中刺伤...... 你说你正在使用 MVVM 和 WPF,但我认为你应该首先修改你在这些主题上的背景......

基本上,您的视图应该绑定到 ViewModel 上的属性。

通常您会希望有一个可观察的集合作为列表框的来源,并且在您的 XAML 上执行类似的操作

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

(我假设您有一个名为 ListPropertyNameObservableCollection 类型的属性,显然您会根据需要将其命名为其他名称)

然后,命令。一旦你有:

<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,因此视图会知道您的集合已更改),这就是它的真正意义......

【讨论】:

  • "显然你会根据你的需要给它起个别的名字"
【解决方案2】:

您忘记了 DataContext 吗? 在后面的代码中,您应该添加以下行: this.DataContext = new YourViewModel();

你能展示你的 CommandMethods 吗?

【讨论】:

    【解决方案3】:

    您忘记了将 ItemsSource 绑定到第二个 xaml 中的列表。如果您想以正确的方式使用 MVVM 进行操作,则需要视图模型中的两个 ItemsSource 属性、两个中继命令和两个字符串属性。在命令操作中,只需将适当的文本添加到相应列表的 ItemsSource。

    您还需要了解 INotifyPropertyChanged、ObservableCollection 和 ICommand 才能使其工作。

    【讨论】:

      猜你喜欢
      • 2015-06-07
      • 1970-01-01
      • 1970-01-01
      • 2019-05-20
      • 2012-11-24
      • 2011-04-25
      • 2010-11-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多