MVVM绑定文本框,按钮和列表视图

时间:2017-05-06 13:48:23

标签: c# wpf mvvm

我尝试使用listview绑定文本框(名为Labor)和textblock(显示时间)。列表视图应显示他们在按下按钮后所需的工作和时间名称"添加项目"。我已经尝试过使用observablecollection,但我做错了。 ListView中的数据应分为两列。 我怎么能这样做?

XAML:

data.table

视图模型:

    <Window x:Class="LabourTimer.View.LabourMainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:LabourTimer.View"
        mc:Ignorable="d"
        xmlns:viewmodel="clr-namespace:LabourTimer.ViewModel"        
        Title="Labour timer" Height="600" Width="515">
    <Window.Resources>
        <viewmodel:LabourViewModel x:Key="viewModel"/>

    </Window.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <StackPanel>
            <Label Content="Timer:" Grid.Row="0"/>
            <TextBlock Text="{Binding CurrentTime}" Grid.Row="0"/>
            <Label Content="Labour name:"/>
            <TextBox x:Name="Labour" Text="{Binding labourName, Mode=TwoWay}"/>
        </StackPanel>
        <StackPanel Grid.Column="1">
            <Button Content="Start/Stop" Command="{Binding StartStopCommand}"/>
            <Button Content="Reset" Command="{Binding ResetCommand}"/>
            <Button Content="Add to list" Command="{Binding AddCommand}"/>
            <Button Content="Remove from list"/>

        </StackPanel>
        <ListView  x:Name="listView" Height="Auto" Margin="0,5" Grid.Row="1" VerticalAlignment="Top" Grid.ColumnSpan="2" MinWidth="300">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Labour name" Width="400" />
                    <GridViewColumn Header="Time in seconds" Width="100"/>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</Window>

CustomCommand:

namespace LabourTimer.ViewModel
{

    public class LabourViewModel : INotifyPropertyChanged
    {
        private DispatcherTimer _timer;
        private string _currentTime;
        private int _seconds;
        private bool _running;


        public ICommand StartStopCommand { get; set; }
        public ICommand ResetCommand { get; set; }
        public ICommand AddCommand { get; set; }
        public string CurrentTime
        {
            get { return _currentTime; }
            set { _currentTime = value;
                OnPropetyChanged("CurrentTime");
            }
        }


        public LabourViewModel()
        {
            _timer = new DispatcherTimer();
            _timer.Interval = TimeSpan.FromSeconds(1);
            _timer.Tick += new EventHandler(TimerTick);
            _running = false;
            CurrentTime = "0";

            LoadCommands();

        }

        private void LoadCommands()
        {
           StartStopCommand = new CustomCommand(StartStopTimer, CanStartStopTimer);
           ResetCommand = new CustomCommand(ResetTimer, CanResetTimer);
           AddCommand = new CustomCommand(AddLabour, CanAddLabour);
        }

        private void AddLabour(object obj)
        {


        }

        private bool CanAddLabour(object obj)
        {
            return true;
        }

        private bool CanResetTimer(object obj)
        {
            if (CurrentTime != "0")
                return true;
            else
                return false;
        }

        private void ResetTimer(object obj)
        {
            _timer.Stop();
            CurrentTime = "0";
            _seconds = 0;
        }

        private void StartStopTimer(object obj)
        {
            if (_running == false)
            {
                _timer.Start();
                _running = true;
            }
            else
            {
                _timer.Stop();
                _running = false;
            }
        }

        private bool CanStartStopTimer(object obj)
        {
            return true;
        }

        private void TimerTick(object send, EventArgs e)
        {
            _seconds++;
            CurrentTime = _seconds.ToString();
        }


        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropetyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

主窗口:

 public class CustomCommand : ICommand
    {
        private Action<object> execute;
        private Predicate<object> canExecute;

        public CustomCommand(Action<object> execute, Predicate<object> canExecute)
        {
            this.execute = execute;
            this.canExecute = canExecute;
        }

        public event EventHandler CanExecuteChanged
        {
            add
            {
                CommandManager.RequerySuggested += value;
            }
            remove
            {
                CommandManager.RequerySuggested -= value;
            }
        }


        public bool CanExecute(object parameter)
        {
            bool b = canExecute == null ? true : canExecute(parameter);
            return b;
        }

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

0 个答案:

没有答案