【问题标题】:Hide list view until button is pressed隐藏列表视图,直到按下按钮
【发布时间】:2014-01-21 09:29:24
【问题描述】:

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

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

【问题讨论】:

    标签: c# wpf xaml listview mvvm


    【解决方案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>
    

    【讨论】:

    • 仅供参考 - WPF 默认提供 BooleanToVisibilityConverter。只需像这样声明:&lt;BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/&gt;.
    • 感谢您的帮助!投票支持我使用您的代码及其工作,但是现在当我单击按钮时我获取数据但按钮已启用在这种情况下我应该更改什么?顺便说一句除了 {return goCommand ?? (goCommand=new MyCommand(()=>Command(),true)); ,也许以简单的方式?提前致谢
    • @RohitVats- 当我更改为这种类型时,我在 RT 中遇到错误,我是否需要添加一些除此之外的内容
    • 在 MyCommand 的第二个参数中不会是真的它就像 get{return goCommand ?? (goCommand=new MyCommand(()=>Command(),()=>!isDataLoaded));}
    • @Rohit 谢谢哥们不知道。
    【解决方案2】:

    加载您的表单,然后放入“listView1.hide()”。

    然后创建你的按钮事件。

    键入“listView1.show()”。

    附:您还可以在您的 c# 代码中设置所有这些值。

    【讨论】:

      【解决方案3】:

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

      附带说明一下,您的 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}" />
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-07-24
        • 1970-01-01
        • 2012-05-01
        • 1970-01-01
        • 2011-03-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多