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

时间:2014-01-03 16:29:37

标签: c# wpf xaml listview mvvm

我有文本框和列表视图,当您按下按钮时 你是列表视图填充数据,目前列表视图下的按钮和文本框和 按下按钮后总是在那里并填满。 有一种方法可以隐藏页面中的列表视图,直到您按下按钮并请求数据?

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();

    }

......

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}" }" >
}

3 个答案:

答案 0 :(得分:1)

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

作为旁注,您的ViewModel未实现 INotifyPropertyChanged ,因此您还需要在更改属性时更新visiblity:

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}" />

答案 1 :(得分:1)

  

视图模型

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));
    }
}
  

BooleanToVisibilityConverter

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();
    }
}
  

XAML

<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>

答案 2 :(得分:0)

然后在put“listView1.hide()”中加载你的表单。

然后创建按钮事件。

输入“listView1.show()”。

P.S。您还可以在c#代码中设置所有这些值。