C# 隐藏列表视图,直到按下按钮

C# 隐藏列表视图,直到按下按钮,c#,wpf,xaml,listview,mvvm,C#,Wpf,Xaml,Listview,Mvvm,我有文本框和列表视图,当你按下按钮时 如果列表视图中填充了数据,则当前列表视图位于按钮和文本框下,并且 在你按下按钮后,它总是在那里并且充满了。 有一种方法可以从页面隐藏列表视图,直到您按下按钮并请求数据 public class ModelView { public ModelView() { GetServiceCollection = new ObservableCollection<string>(); } bool isDa

我有文本框和列表视图,当你按下按钮时 如果列表视图中填充了数据,则当前列表视图位于按钮和文本框下,并且 在你按下按钮后,它总是在那里并且充满了。 有一种方法可以从页面隐藏列表视图,直到您按下按钮并请求数据

public class ModelView
{
    public ModelView()
    {
        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();

    }

......
公共类模型视图
{
公共模型视图()
{
GetServiceCollection=新的ObservableCollection();
}
bool isDataLoaded=错误;
MyCommand-goCommand;
公共ICommand命令
{
获取{return goCommand??(goCommand=new MyCommand(()=>OnGoCommand(),()=>!isDataLoaded));}
}
公共ObservableCollection GetServiceCollection{get;set;}
无效OnGoCommand()
{
GetServiceCollection.Clear();
foreach(配置ServiceModel.CollectList中的变量项)
{
GetServiceCollection.Add(项);
}
isDataLoaded=true;
goCommand.RaiseCanExecuteChanged();
}
......
xaml

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

<ListView Grid.Column="2" HorizontalAlignment="Center" Height="230" 
 Margin="5,20,0,0" Grid.Row="2" VerticalAlignment="Top" Width="330" 
 ItemsSource="{Binding GetCollection}" }" >
}


这里最好的选择是在ViewModel上创建另一个属性,将ListView的可见性绑定到该属性。在GoCommand实现中,将此属性设置为visible

作为补充说明,ViewModel未实现INotifyPropertyChanged
,因此在更改属性时,您还需要这样做以更新可见性:

private Visibility listViewVisibility;
public Visibility ListViewVisibility
{
    get { return listViewVisibility; }
    set
    {
        if (this.listViewVisibility == value)
            return;

        this.listViewVisibility = value;
        this.OnPropertyChanged("ListViewVisibility");
    }
}

public event PropertyChangedEventHandler PropertyChanged;

protected void OnPropertyChanged(string propertyName)
{
    if(this.PropertyChanged != null)
        this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
xaml:

<ListView Grid.Column="2" HorizontalAlignment="Center" Height="230" 
          Margin="5,20,0,0" Grid.Row="2" VerticalAlignment="Top" Width="330" 
          Visibility="{Binding ListViewVisibility}"
          ItemsSource="{Binding GetCollection}" />

我们将加载表单,然后放入“listView1.hide()”中

然后创建按钮事件

键入“listView1.show()”

另外,您还可以在c代码中设置所有这些值

视图模型

xaml



FYI-
BooleantVisibilityConverter
默认在WPF中提供。只需声明如下:
。感谢您的帮助!我使用了您的代码及其工作方式,但现在当我单击按钮时,我获得了数据,但按钮已启用。在这种情况下,我应该更改什么?顺便说一句,有一种编写代码的方法除了{return goCommand???(goCommand=new MyCommand(()=>Command(),true));之外,可能还需要简单的方法?谢谢advance@RohitVats-当我更改为这种类型时,我在RT中遇到了错误,我需要添加一些额外的内容吗?它在MyCommand的第二个参数中不会为true,它将像get{return goCommand??(goCommand=new MyCommand()=>Command(),()=>!isDataLoaded();}@Rohit谢谢,巴迪不知道。
public class ConfigModelView:INotifyPropertyChanged
{ 
    public ConfigModelView()
    {
        GetServiceCollection=new ObservableCollection<string>();
    }

    bool isDataLoaded;
    public bool IsDataLoaded
    {
        get { return isDataLoaded; }
        set { isDataLoaded = value; OnPropertyChanged("IsDataLoaded"); }
    }
    MyCommand goCommand;
    public ICommand GoCommand
    {
        get{return goCommand ?? (goCommand=new MyCommand(()=>Command(),()=>!isDataLoaded));}
    }
    public ObservableCollection<string> GetServiceCollection{get;set;}

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

        isDataLoaded = true;
        OnPropertyChanged("IsDataLoaded");
        goCommand.RaiseCanExecuteChanged();
    }


    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}
public class BoolToVisibilityConverter : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value is bool)
        {
            if ((bool)value)
                return Visibility.Visible;
            else
                return Visibility.Collapsed;
        }
        return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
<Window x:Class="WpfApplication3.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication3"
    Title="Window1" Height="300" Width="800">
<Window.Resources>
    <local:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter"/>
</Window.Resources>
<StackPanel>
    <Button Content="Go" Grid.Column="3" Grid.Row="1" HorizontalAlignment="Left"
VerticalAlignment="Top" Width="75" Height="21.96" Command="{Binding GoCommand}"/>

    <ListView Grid.Column="2" HorizontalAlignment="Center" Height="230" 
 Margin="5,20,0,0" Grid.Row="2" VerticalAlignment="Top" Width="330" 
 Visibility="{Binding IsDataLoaded,
Converter= {StaticResource BoolToVisibilityConverter}}"
ItemsSource="{Binding GetCollection}" />
</StackPanel>