C# and Silverlight

本文关键字:Silverlight and | 更新日期: 2023-09-27 17:59:09

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

我怎么能让Silverlight中的xaml包含一个textbox=Name和两个按钮:Next和Back。当我点击Next时,textbox="Name2",当我点击Back时,textbox="Name1">

非常感谢。

C# and Silverlight

按如下方式创建XAML:

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

如果您想要一个简单的解决方案,请编写在代码隐藏中更新TextBlock文本代码的逻辑。

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

但是,如果它是一个严肃的应用程序,并且随着时间的推移,应用程序的范围越来越复杂,您可能需要使用MVVM模式,如下所示:

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

您可以在按钮的点击事件中设置文本框的文本。所以XAML将是这样的。

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

当页面被初始化时,您需要将文本添加到文本框中,这样初始化代码就会转到这里。

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

然后你需要在点击事件中有这样的逻辑。

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