【问题标题】:Display the list view on button command在按钮命令上显示列表视图
【发布时间】:2014-01-02 17:10:34
【问题描述】:

我有以下视图模型的 xaml 和代码,目前我将屏幕列表视图绑定到视图模型。 用户控件有文本框和按钮,当用户单击按钮(Go)时,我想从视图中获取数据,我应该怎么做?

目前我总是在运行窗口时获取数据,但是

我希望在打开页面和单击时列表为空 GO按钮列表将被填充

<Grid  Width="877" Height="632" 
DataContext="{Binding Source={StaticResource ConfigServiceModelViewDataSource}}" >
<Grid.ColumnDefinitions>
<UserControl.Resources>
    <ViewModel:ConfigServiceModelView x:Key="ConfigServiceModelViewDataSource" />
    <DataTemplate x:Key="CollectionTemplate">
    </DataTemplate>
</UserControl.Resources>
<ListView Grid.Column="2" HorizontalAlignment="Center" Height="230" 
 Margin="5,20,0,0" Grid.Row="2" VerticalAlignment="Top" Width="330" 
 ItemsSource="{Binding GetCollection}" }" >
<Button Content="Go" Grid.Column="3" Grid.Row="1" HorizontalAlignment="Left"
  VerticalAlignment="Top" Width="75" Height="21.96" />

ModelView 我从模型中获取数据,比如

internal class ConfigModelView {
   private  ConfigServiceModel _configServiceModel = new ConfigServiceModel();

   public List<string> GetServiceCollection {
      get { 
         return _configServiceModel.CollectList; 
      }
   }
}

【问题讨论】:

  • I want to get the data from the view ,how should I do that - 使用数据绑定。顺便说一句,你的问题完全不清楚,你能澄清一下吗?
  • 在这种情况下,仅在单击按钮时才填充您的列表。
  • @HighCore- 已更新,希望现在清楚...

标签: c# wpf xaml mvvm


【解决方案1】:

试试这个

视图模型

public class ConfigModelView
{
    public ConfigModelView()
    {
        GetServiceCollection = new ObservableCollection<string>();
    }

    bool isDataLoaded = false;

    MyCommand goCommand;
    public ICommand GoCommand
    {
        get { return goCommand ?? (goCommand = new MyCommand(() => OnGoCommand(), () => !isDataLoaded)); }
    }

    public ObservableCollection<string> GetServiceCollection { get; set; }

    void OnGoCommand()
    {
        GetServiceCollection.Clear();

        foreach (var item in _configServiceModel.CollectList)
        {
            GetServiceCollection.Add(item);
        }

        isDataLoaded = true;
        goCommand.RaiseCanExecuteChanged();

    }
}

自定义命令。你可以使用 RelayCommand

public class MyCommand : ICommand
{
    private Action _action;
    private Func<bool> _canExecute;
    public MyCommand(Action action, Func<bool> canExecute)
    {
        _action = action;
        _canExecute = canExecute;
    }

    public bool CanExecute(object parameter)
    {
        return _canExecute();
    }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
        _action();
    }

    public void RaiseCanExecuteChanged()
    { 
        if(CanExecuteChanged!=null)
            CanExecuteChanged(this,new EventArgs());
    }
}

xaml

<Button Content="Go" Grid.Column="3" Grid.Row="1" HorizontalAlignment="Left"
VerticalAlignment="Top" Width="75" Height="21.96" Command="{Binding GoCommand}"/>

我希望这会有所帮助。

【讨论】:

  • 感谢您的帮助,投了赞成票命令 new MyCommand(()=>Command(),true)) 中的错误
  • 顺便说一句,在我按下 GO 按钮并获取数据后,有一种方法可以禁用它吗?
  • 是的,缺少一个括号我将其复制并粘贴到 VS 我得到了同样的错误。我更新了答案
  • 是的,一旦获取数据,很容易通过命令禁用。我根据您的要求更新了整个答案。启用禁用魔法将通过设置 isDataLoaded true 或 false 然后调用 goCommand.RaiseCanExecuteChanged(); 来设置。您需要了解命令的工作原理,您会发现它非常简单。
  • 谢谢专家你是假设,你能解释一下按钮是如何被禁用的,因为我无法理解它,我没有看到你指的是布尔按钮的任何地方,
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-20
  • 1970-01-01
  • 1970-01-01
  • 2016-04-15
  • 1970-01-01
相关资源
最近更新 更多