本文介绍了单击按钮时如何在文本框中添加值添加从组合框中选择的selecteditem值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 private List<ListItem>  _tyreList;
public List<ListItem> TyreList;
{
  get
    {
                return _tyre;
     }
    set
      {
                _tyre= value;
                RaisePropertyChanged();
      }
}

<ComboBox Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="1"  SelectedValuePath="Value" DisplayMemberPath="Name" ItemsSource="{Binding ElementName=LayoutRoot, Path=DataContext.TyreList}" SelectedItem="{Binding TyreList}"/>

                                        
<Button Command="{Binding AddTyreCommand}" Grid.Row="3" Grid.Column="4" Grid.ColumnSpan="2" Content="Add Tyre"  Width="150" Height="25" HorizontalAlignment="Left" />
                                        
<TextBox  Name="tbMultiLine"  TextWrapping="Wrap"  AcceptsReturn="True"  VerticalScrollBarVisibility="Visible" Text="{Binding what binding to set here => TyreList}" />





我尝试过:





What I have tried:

private RelayCommand _addTyreCommand;
public RelayCommand AddTyreCommand => _addTyreCommand?? (_addTyreCommand= new RelayCommand(AddTyres , CanAddTyres));
   
private void AddTyres ()
{
//something here

 }      

 private bool CanAddTyres() => true;//to add tyres

推荐答案

public ListItem SelectedItem 
{
  get=>selectedItem;
  set
     {
      selectedItem=value;
      RaisePropertyChange();
     }
}




<TextBox Text = "{Binding SelectedItem, UpdateSourceTrigger=PropertyChanged}"/>







第二次使用RelayCommand接受命令参数,例如




Second use RelayCommand which accepts command parameter like

RelayCommand<object> AddtyreCommand=> -----new RelayCommand(Addtyres)
where 
private void AddTyres(object item)
{
   SelectedItem  = item as ListItem;
.......
}







第三套名称to combobox和In Button定义CommandParameter




Third set Name to combobox and In Button Define CommandParameter

<Button .... CommandParameter="{Binding ElementName=comboTyreList, Path=SelectedItem" />





多数民众赞成。你完成了。

对不适当的格式和代码抱歉。



Thats it. You are done.
Sorry for inappropriate formatting and code.


这篇关于单击按钮时如何在文本框中添加值添加从组合框中选择的selecteditem值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 07:13