gpt4 book ai didi

c# - 在 WPF ComboBox 中设置默认值

转载 作者:行者123 更新时间:2023-11-30 12:09:24 27 4
gpt4 key购买 nike

我正在使用 ComboBox ItemsSource 属性绑定(bind)将列表中的项目显示到组合框。

代码如下:

<ComboBox x:Name="Cmb_Tax" ItemsSource="{Binding TaxList}" 
DisplayMemberPath="ChargeName" SelectedItem="{Binding
SelectedTax,UpdateSourceTrigger=PropertyChanged}" IsEditable="True"
IsTextSearchEnabled="True" SelectionChanged="Cmb_Tax_SelectionChanged"/>

Classes.Charges _selected_tax = new Classes.Charges();
public Classes.Charges SelectedTax
{
get
{
return _selected_tax;
}
set
{
_selected_tax = value;
}
}

List<Classes.Charges> _taxlist = new List<Classes.Charges>();
public List<Classes.Charges> TaxList
{
get
{
return _taxlist;
}
set
{
_taxlist = value;
OnPropertyChanged("TaxList");
}
}

它正确显示组合框中的项目。

在 TaxList "No Tax" 中有一个特定的项目,我想在组合框中默认选择它。该项目可以出现在列表中的任何索引处(不需要列表的第一个或最后一个项目)。

我正在尝试使用以下代码来设置组合框的选定索引属性,但遗憾的是它不起作用。

TaxList = Classes.Charges.GetChargeList("Tax");
Cmb_Tax.DataContext = this;
int i = TaxList.FindIndex(x => x.ChargeName == tax_name);
Cmb_Tax.SelectedIndex = i;

方法 FindIndex() 正确返回 "No Tax" 的索引,但是当我尝试将其分配给组合 SelectedIndexSelectedIndex 时> 不变。它保持在 -1。

更新1

private void Cmb_Tax_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
MessageBox.Show(SelectedTax.ChargeName);
}

更新2按照@ElectricRouge 的建议更新了代码

<ComboBox x:Name="Cmb_Tax" ItemsSource="{Binding TaxList, Mode=TwoWay}" 
DisplayMemberPath="ChargeName" SelectedItem="{Binding SelectedTax,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"
IsEditable="True" IsTextSearchEnabled="True"
SelectionChanged="Cmb_Tax_SelectionChanged"/>


Classes.Charges _selected_tax = new Classes.Charges();
public Classes.Charges SelectedTax
{
get
{
return _selected_tax;
}
set
{
_selected_tax = value;
OnPropertyChanged("SelectedTax");
}
}

ObservableCollection<Classes.Charges> _taxlist = new ObservableCollection<Classes.Charges>();
public ObservableCollection<Classes.Charges> TaxList
{
get
{
return _taxlist;
}
set
{
_taxlist = value;
OnPropertyChanged("TaxList");
}
}

public void Load_Tax(string tax_name = null, Classes.Charges selected_tax = null)
{
TaxList = Classes.Charges.GetParticularChargeList("Tax");
Cmb_Tax.DataContext = this;
//Cmb_Tax.SelectedValue = tax_name;
SelectedTax = selected_tax;
//int i = TaxList.FindIndex(x => x.ChargeName == tax_name);
//Cmb_Tax.SelectedIndex = i;
}

知道为什么一定会发生这种情况吗?还请建议在组合框中显示默认值的任何其他方法。

最佳答案

这是一个工作示例:

View 模型:

 public MainWindow()
{
InitializeComponent();

var vm = new ViewModel();
this.DataContext = vm;
this.Loaded += (o,e) => vm.LoadData();
}

public class ViewModel : INotifyPropertyChanged
{
private IList<Charges> taxList;
public ICollectionView TaxList { get; private set; }

public void LoadData()
{
taxList = Charges.GetChargeList("taxes");

TaxList = CollectionViewSource.GetDefaultView(taxList);
RaisePropertyChanged("TaxList");

TaxList.CurrentChanged += TaxList_CurrentChanged;

var noTax = taxList.FirstOrDefault(c => c.ChargeName == "No Tax");
TaxList.MoveCurrentTo(noTax);
}

void TaxList_CurrentChanged(object sender, EventArgs e)
{
var currentCharge = TaxList.CurrentItem as Charges;
if(currentCharge != null)
MessageBox.Show(currentCharge.ChargeName);
}



public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}

查看:

<ComboBox x:Name="cboTaxList" 
ItemsSource="{Binding TaxList}"
DisplayMemberPath="ChargeName"
IsSynchronizedWithCurrentItem="True" />

关于c# - 在 WPF ComboBox 中设置默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21535747/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com