如何绑定组合框的项目 [英] How to bind the items of combobox
问题描述
嗨
i是wpf mvvm prism的新手。
这里的项目是静态项目(未恢复和恢复和取消)。
当前的combobox项目来自数据库(即其中一个静态项目)
代码是
ActionItems = null;
ActionItems = new O bservableCollection< ViewRequestStatusType>();
ActionType = new ViewRequestStatusType();
ActionType.Id = 1;
ActionType.Type =取消;
ActionItems.Add(ActionType);
ActionType = new ViewRequestStatusType();
ActionType.Id = 2;
ActionType.Type =Not Resumed;
ActionItems.Add(ActionType);
ActionType = new ViewRequestStatusType();
ActionType.Id = 3;
ActionType.Type =Resumption;
ActionItems.Add(ActionType);
ActionType = new ViewRequestStatusType();
ActionType.Id = 0;
ActionType.Type = obj.Data.Action;
ActionItems.Add(ActionType);
var cnt = ActionItems.Count;
ActionItems.Move(cnt - 1,0);
Xaml代码是
< combobox style ={StaticResource ValidateComboBox}isenabled ={Binding Iss如何}fontsize =25itemssource ={Binding ActionItems}>
SelectedItem ={Binding ActionType,Mode = TwoWay,UpdateSourceTrigger = PropertyChanged,ValidatesOnDataErrors = True}
DisplayMemberPath =TypeMargin =10,0,0,0SelectedValuePath =IdIsSynchronizedWithCurrentItem =True
Height =40Width =600/> ;
输出为
未恢复
取消
未恢复
恢复
任何人都可以帮助我
提前致谢
Hi
i am new to wpf mvvm prism.
Here the items are static items(Not Resumed and Resumption and Cancellation).
current item of combobox is coming from database(i.e one of the static items)
The code is
ActionItems = null;
ActionItems = new O bservableCollection<ViewRequestStatusType>();
ActionType = new ViewRequestStatusType();
ActionType.Id = 1;
ActionType.Type = "Cancellation";
ActionItems.Add(ActionType);
ActionType = new ViewRequestStatusType();
ActionType.Id = 2;
ActionType.Type = "Not Resumed";
ActionItems.Add(ActionType);
ActionType = new ViewRequestStatusType();
ActionType.Id = 3;
ActionType.Type = "Resumption";
ActionItems.Add(ActionType);
ActionType = new ViewRequestStatusType();
ActionType.Id = 0;
ActionType.Type = obj.Data.Action;
ActionItems.Add(ActionType);
var cnt = ActionItems.Count;
ActionItems.Move(cnt - 1, 0);
Xaml code is
<combobox style="{StaticResource ValidateComboBox}" isenabled="{Binding Isshow }" fontsize="25" itemssource="{Binding ActionItems}">
SelectedItem="{Binding ActionType,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,ValidatesOnDataErrors=True}"
DisplayMemberPath="Type" Margin="10,0,0,0" SelectedValuePath="Id" IsSynchronizedWithCurrentItem="True"
Height="40" Width="600" />
The output is
Not Resumed
Cancellation
Not Resumed
Resumption
Can anyone please help me
Thanks in advance
推荐答案
xaml:
< ; window x:class =WpfApplication6.Window1xmlns:x =#unknown>
xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x =http://schemas.microsoft.com/winfx/2006/xaml
Title =Window1Hei ght =300宽度=300>
< grid>
< stackpanel>
asdf
< combobox itemssource ={Binding Path = PhonebookEntries}>
DisplayMemberPath =Name
SelectedValuePath =姓名
SelectedValue ={Binding Path = PhonebookEntry}/>
命名空间WpfApplication6
{
///< summary>
/// Window1.xaml的交互逻辑
///
public partial class Window1:Window
{
public Window1()
{
InitializeComponent();
ConnectionViewModel vm = new ConnectionViewModel();
DataContext = vm;
}
private void Button_Click(对象发送者,RoutedEventArgs e)
{
((连接ionViewModel)DataContext).PhonebookEntry =test;
}
}
公共类PhoneBookEntry
{
public string Name {get;组; }
公共PhoneBookEntry(字符串名称)
{
姓名=姓名;
}
}
公共类ConnectionViewModel:INotifyPropertyChanged
{
public ConnectionViewModel()
{
IList< phonebookentry> list = new List< phonebookentry>();
list.Add(new PhoneBookEntry(test));
list.Add(new PhoneBookEntry(test2) );
_phonebookEntries = new CollectionView(list);
}
private readonly CollectionView _phonebookEntries;
private string _phonebookEntry;
公共CollectionView电话簿条目
{
get {return _phonebookEntries; }
}
公共字符串PhonebookEntry
{
get {return _phonebookEntry ; }
set
{
if(_phonebookEntry == value)return;
_phonebookEntry = value;
OnPropertyChanged(PhonebookEntry);
}
}
private void OnPropertyChanged(string propertyName)
{
if(PropertyChanged!= null)
PropertyChanged(this,new PropertyChangedEventArgs(propertyName));
}
公共事件PropertyChangedEventHandler PropertyChanged;
}
}
xaml:
<window x:class="WpfApplication6.Window1" xmlns:x="#unknown">
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<grid>
<stackpanel>
asdf
<combobox itemssource="{Binding Path=PhonebookEntries}">
DisplayMemberPath="Name"
SelectedValuePath="Name"
SelectedValue="{Binding Path=PhonebookEntry}" />
namespace WpfApplication6
{
/// <summary>
/// Interaction logic for Window1.xaml
///
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
ConnectionViewModel vm = new ConnectionViewModel();
DataContext = vm;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
((ConnectionViewModel)DataContext).PhonebookEntry = "test";
}
}
public class PhoneBookEntry
{
public string Name { get; set; }
public PhoneBookEntry(string name)
{
Name = name;
}
}
public class ConnectionViewModel : INotifyPropertyChanged
{
public ConnectionViewModel()
{
IList<phonebookentry> list = new List<phonebookentry>();
list.Add(new PhoneBookEntry("test"));
list.Add(new PhoneBookEntry("test2"));
_phonebookEntries = new CollectionView(list);
}
private readonly CollectionView _phonebookEntries;
private string _phonebookEntry;
public CollectionView PhonebookEntries
{
get { return _phonebookEntries; }
}
public string PhonebookEntry
{
get { return _phonebookEntry; }
set
{
if (_phonebookEntry == value) return;
_phonebookEntry = value;
OnPropertyChanged("PhonebookEntry");
}
}
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
这篇关于如何绑定组合框的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!