C# 在WPF组合框中设置默认值
我正在使用ComboBox ItemsSource属性绑定将列表中的项目显示到组合框 代码如下:C# 在WPF组合框中设置默认值,c#,.net,wpf,C#,.net,Wpf,我正在使用ComboBox ItemsSource属性绑定将列表中的项目显示到组合框 代码如下: <ComboBox x:Name="Cmb_Tax" ItemsSource="{Binding TaxList}" DisplayMemberPath="ChargeName" SelectedItem="{Binding SelectedTax,UpdateSourceTrigger=PropertyChanged}" IsEditabl
<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");
}
}
方法FindIndex()正确返回的索引“No Tax”
,但当我尝试将其分配给组合的SelectedIndex
时,SelectedIndex
不会改变。它保持在-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;
}
Classes.Charges _selected_tax=新Classes.Charges();
公共课。收费选择税
{
得到
{
返回所选的税;
}
设置
{
_所选税费=价值;
关于财产变更(“选择税”);
}
}
ObservableCollection_taxlist=新的ObservableCollection();
公共可观测收集税单
{
得到
{
报税表;
}
设置
{
_taxlist=价值;
不动产变更(“税单”);
}
}
公共无效加载税(字符串Tax\u name=null,类别。所选费用\u Tax=null)
{
TaxList=Classes.Charges.GetSpecialChargeList(“税”);
Cmb_Tax.DataContext=此;
//Cmb\u Tax.SelectedValue=税名;
SelectedTax=所选的税;
//inti=TaxList.FindIndex(x=>x.ChargeName==tax\u name);
//Cmb_Tax.SelectedIndex=i;
}
知道为什么会这样吗?
另外,请建议在组合框中显示默认值的任何其他方法。列表未实现
INotifyCollectionChanged
使其成为可观察的集合
ObservableCollection<Classes.Charges> _taxlist = new ObservableCollection<Classes.Charges>();
public ObservableCollection<Classes.Charges> TaxList
{
get
{
return _taxlist;
}
set
{
_taxlist = value;
OnPropertyChanged("TaxList");
}
}
以下是一个工作示例: 视图模型:
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));
}
}
}
}
public主窗口()
{
初始化组件();
var vm=new ViewModel();
this.DataContext=vm;
this.Loaded+=(o,e)=>vm.LoadData();
}
公共类视图模型:INotifyPropertyChanged
{
私人IList税单;
public ICollectionView TaxList{get;private set;}
公共void LoadData()
{
taxList=费用。GetChargeList(“税款”);
TaxList=CollectionViewSource.GetDefaultView(TaxList);
RaisePropertyChanged(“税单”);
TaxList.CurrentChanged+=TaxList\u CurrentChanged;
var noTax=taxList.FirstOrDefault(c=>c.ChargeName==“无税”);
TaxList.MoveCurrentTo(noTax);
}
void TaxList\u CurrentChanged(对象发送方,事件参数e)
{
var currentCharge=TaxList.CurrentItem作为费用;
如果(currentCharge!=null)
MessageBox.Show(currentCharge.ChargeName);
}
公共事件属性更改事件处理程序属性更改;
public void RaisePropertyChanged(字符串propertyName)
{
if(PropertyChanged!=null)
{
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
}
}
}
视图:
我很想知道您的Cmb\u Tax\u选择改变了什么
方法。您能否提供这些信息以便我们更好地了解问题?您是否尝试设置您的SelectedTax
成员(SelectedItem绑定所指示的内容),而不是设置组合框的SelectedIndex
?尝试获取并向SelectedTax分配包含“无税”的对象,但仍然没有成功:(我将考虑执行<代码> NoTyFyPrimyType < /Cord>)并从SETER中提高<代码> PrimyType 事件。例如,它可能会变成一个类似于代码> >选择税号= TraceListFiStRealStur缺额。(…执行此操作后,我是否应将值分配给SelectedTax?它是否会反映在combo中?尝试了您的代码并尝试将值分配给SelectedTax。在combo中未选择任何值是的还应在SelectedTax上实现Changed
类似的设置{{u selected_tax=value;OnPropertyChanged(“SelectedTax”);}
我已经用修改过的代码更新了帖子。它仍然不起作用
SelectedItem="{Binding SelectedTax,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"
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" />