Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从视图中获取数据_C#_Wpf - Fatal编程技术网

C# 如何从视图中获取数据

C# 如何从视图中获取数据,c#,wpf,C#,Wpf,我使用MVVM来构建我的项目,现在我遇到了一些问题,当我单击一个按钮时,我想从视图到viewmodel获取数据,我应该怎么做 谢谢 Bob将该数据绑定到视图模型,并在用户单击按钮时执行命令。命令和数据存储在视图模型中,因此它拥有所需的一切 public class YourViewModel : ViewModel { private readonly ICommand doSomethingCommand; private string data; public Yo

我使用MVVM来构建我的项目,现在我遇到了一些问题,当我单击一个按钮时,我想从视图到viewmodel获取数据,我应该怎么做

谢谢
Bob将该数据绑定到视图模型,并在用户单击按钮时执行命令。命令和数据存储在视图模型中,因此它拥有所需的一切

public class YourViewModel : ViewModel
{
    private readonly ICommand doSomethingCommand;
    private string data;

    public YourViewModel()
    {
        this.doSomethingCommand = new DelegateCommand(this.DoSomethingWithData);
    }

    public ICommand DoSomethingCommand
    {
        get { return this.doSomethingCommand; }
    }

    public string Data
    {
        get { return this.data; }
        set
        {
            if (this.data != value)
            {
                this.data = value;
                this.OnPropertyChanged(() => this.Data);
            }
        }
    }

    private void DoSomethingWithData(object state)
    {
        // do something with data here
    }
}
XAML:

您的“主”视图模型将公开这些项的集合(通常是一个
observedcollection
):


在我看来,Kent建议的解决方案是迄今为止遵循MVVM的最佳/唯一的解决方案。 但是,如果您不想将列表框选择复制/反映到视图模型,或者您想要一个快速且(根据MVVM)脏的解决方案,则可以使用命令参数将数据从视图发送到视图模型

为此,必须将按钮的
CommandParameter
属性绑定到包含要发送到视图模型的数据的属性。为了简单起见,我只使用了一个
文本框

<StackPanel Orientation="Vertical">
    <TextBox x:Name="Data"/>
    <Button Content="DoSomething" 
            Command="{Binding Path=DoSomethingCommand}" 
            CommandParameter="{Binding ElementName=Data, Path=Text}"/>
</StackPanel>
这样,您将获得指定的内容作为
ICommand
Execute
方法中的参数

public class MyCommand : ICommand
{
    public bool CanExecute(object parameter)
    {
        return true;
    }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
        string dataFromView = (string)parameter;

        // ...
        MessageBox.Show(dataFromView);
    }
}

嗨,肯特:也许我的描述不太熟悉我目前的问题,在我看来,我有一个列表框,我在这个列表框中选择了一些项目,然后我点击一个按钮,我想让viewmodel获得选择的项目我该怎么做?相应地编辑了我的答案。
public ICollection<CustomerViewModel> Customers
{
    get { return this.customers; }
}
<ListBox ItemsSource="{Binding Customers}">
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="IsSelected" Value="{Binding IsSelected}"/>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>
var selectedCustomers = this.Customers.Where(x => x.IsSelected);
<StackPanel Orientation="Vertical">
    <TextBox x:Name="Data"/>
    <Button Content="DoSomething" 
            Command="{Binding Path=DoSomethingCommand}" 
            CommandParameter="{Binding ElementName=Data, Path=Text}"/>
</StackPanel>
public class ViewModel
{
    private ICommand doSomethingCommand = new MyCommand();

    public ICommand DoSomethingCommand
    {
        get
        {
            return doSomethingCommand;
        }
    }
}
public class MyCommand : ICommand
{
    public bool CanExecute(object parameter)
    {
        return true;
    }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
        string dataFromView = (string)parameter;

        // ...
        MessageBox.Show(dataFromView);
    }
}