开发者

C# and Silverlight

开发者 https://www.devze.com 2023-03-12 13:02 出处:网络
public List<Client> GetClients() { List<Client> clients = new List<Client>(); clients.Add(new Client()
    public List<Client> GetClients()
     {
         List<Client> clients = new List<Client>();
         clients.Add(new Client()
         {
      开发者_如何学Go      Name = "Name1",
         });
         clients.Add(new Client()
         {
            Name = "Name2",
         });
         return clients;
     }

How could I make a xaml in Silverlight contains a textbox=Name and two button: Next and Back. When I click Next the textbox = "Name2" and when I click Back the textbox = "Name1"

Thank you very much.


Create XAML as follows:

<Window x:Class="WpfTestApp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" >
<Grid>
    <TextBlock Name="txtSelectedName" Width="100" Height="30" Margin="113,110,290,171" />
    <Button Name="btnPrevious" Width="100" Height="30" Margin="46,159,357,122" Content="Previous" Click="btnPrevious_Click" />
    <Button Name="btnNext" Width="100" Height="30" Margin="192,158,211,122" Content="Next" Click="btnNext_Click" />
</Grid>

If you want a simple solution write the logic to update TextBlock text code in code behind.

    private int _index = 0;

    public MainWindow()
    {
        InitializeComponent();
        _clients = GetClients();
        txtSelectedName.Text = _clients[_index].Name;

    }
    private void btnPrevious_Click(object sender, RoutedEventArgs e)
    {
        _index = _index == 0 ? _clients.Count - 1 : _index - 1;
        txtSelectedName.Text = _clients[_index].Name;
    }

    private void btnNext_Click(object sender, RoutedEventArgs e)
    {
        _index = _index == _clients.Count - 1 ? 0 : _index + 1;
        txtSelectedName.Text = _clients[_index].Name;
    } 

But if its a serious application and there is scope of the application getting complex over time you may want to use MVVM pattern as follows:

<Window x:Class="WpfTestApp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" >
<Grid>
    <TextBlock Name="txtSelectedName" Width="100" Height="30" Margin="113,110,290,171" Text="{Binding Path=SelectedName}" />
    <Button Name="btnPrevious" Width="100" Height="30" Margin="46,159,357,122" Content="Previous" Command="{Binding Path=Next}" />
    <Button Name="btnNext" Width="100" Height="30" Margin="192,158,211,122" Content="Next" Command="{Binding Path=Previous}" />
</Grid>

 public MainWindow()
    {
        InitializeComponent();
        DataContext = new MainWindowViewModel();
    }




public class MainWindowViewModel :INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private List<Client> _clients;
    private int _index;

    public MainWindowViewModel()
    {
        _clients = GetClients();
        _index = 0;
        SelectedName = _clients[_index].Name;
    }

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

    public List<Client> GetClients()
    {
        List<Client> clients = new List<Client>();
        clients.Add(new Client()
        {
            Name = "Name1",
        });
        clients.Add(new Client()
        {
            Name = "Name2",
        });
        return clients;
    }

    private string _selectedName;
    public string SelectedName
    {
        get { return _selectedName; }
        set
        {
            if(_selectedName != value)
            {
                _selectedName = value;
                OnPropertyChange("SelectedName");
            }
        }
    }


    private RelayCommand _next;
    public RelayCommand Next
    {
        get
        {
            return _next ?? (_next = new RelayCommand(param => this.SetNextName()));
        }
    }

    private void SetNextName()
    {
        _index = _index == _clients.Count - 1 ? 0 : _index + 1;
        SelectedName = _clients[_index].Name;
    }

    private RelayCommand _previous;
    public RelayCommand Previous
    {
        get
        {
            return _previous ?? (_previous = new RelayCommand(param => this.SetPreviousName()));
        }
    }

    private void SetPreviousName()
    {
        _index = _index == 0 ? _clients.Count - 1 : _index - 1;
        SelectedName = _clients[_index].Name;
    }
}

public class RelayCommand : ICommand
{
    private Action<object> _execute;
    private Predicate<object> _canexecute;

    public RelayCommand(Action<object> execute) : this(execute, null){}

    public RelayCommand(Action<object> execute, Predicate<object> canExecute)
    {
        if (execute == null)
            throw new ArgumentException("execute");

        _execute = execute;
        _canexecute = canExecute;
    }

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

    public bool CanExecute(object parameter)
    {
        return _canexecute == null || _canexecute(parameter);
    }

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


you set the text of textbox on click event of the buttons. So XAML will be like this.

<TextBox Height="23" HorizontalAlignment="Left" Margin="166,144,0,0" Name="txtMain" VerticalAlignment="Top" Width="188"  />
            <Button Content="Back" Height="23" HorizontalAlignment="Left" Margin="174,238,0,0" Name="btnBack" VerticalAlignment="Top" Width="75" Click="btnBack_Click" />
            <Button Content="Next" Height="23" HorizontalAlignment="Right" Margin="0,238,286,0" Name="btnNext" VerticalAlignment="Top" Width="75" Click="btnNext_Click" />

When page is initialized then you need to add text to textbox so the initialization code goes here.

public List<Client> _clients = new List<Client>();
        private int _index = 0;

    public MainPage()
            {
                InitializeComponent();

                this.loginContainer.Child = new LoginStatus();
                this._clients = GetClients();
                if(_index<this._clients.Count)
                txtMain.Text = this._clients[_index].Name;
            }

and then you need to have logic in click events like this.

private void btnBack_Click(object sender, RoutedEventArgs e)
    {
        this._index -= 1;

        if (_index >= 0)
            txtMain.Text = this._clients[_index].Name;
        else
            this._index += 1;

    }

    private void btnNext_Click(object sender, RoutedEventArgs e)
    {
        this._index += 1;

        if (_index < this._clients.Count)
            txtMain.Text = this._clients[_index].Name;
        else
            this._index -= 1;
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消