[英]WPF Enum binding to ComboBox
My Enum is not binding to my Contact Manager. 我的枚举未绑定到我的联系人管理器。
I have an Enum Class 我有一个Enum班
[Serializable]
public enum Group
{
Friend,
Family,
Coworker
}
}
Then I have my MainWindow which initializes my Contact Manager 然后我有MainWindow可以初始化我的联系人管理器
public partial class MainWindow : Window
{
//public List<Contact> ContactList = new List<Contact>();
public ObservableCollection<Contact> ContactList = new ObservableCollection<Contact>();
string fileName = null;
Contact furqan = new Contact("Herp Derp");
Contact rizwan = new Contact("Merp Meep");
public MainWindow()
{
InitializeComponent();
myItemsControl.ItemsSource = ContactList;
//myComboBox.ItemsSource = Enum.GetValues(typeof(Group));
//myComboBox.ItemsSource = Enum.GetValues(typeof(Group)).Cast<Group>();
furqan.HomePhone = "801238421";
ContactList.Add(furqan);
ContactList.Add(rizwan);
this.DataContext = this;
}
My XAML looks like this 我的XAML看起来像这样
<Label Grid.Row="7" Content="Home Email:"/>
<TextBox Grid.Row="7" Text="{Binding ElementName=myItemsControl, Path=SelectedItem.PersonalEmail, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<Label Grid.Row="8" Content="Work Email:"/>
<TextBox Grid.Row="8" Text="{Binding ElementName=myItemsControl, Path=SelectedItem.WorkEmail, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<Label Grid.Row="10" Content="Group:"/>
<ComboBox Grid.Row="10" Grid.Column="1" Text="{Binding ElementName=myItemsControl, Path=SelectedItem.ContactGroup, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
The ComboBox should display "CoWorker, Family, Friend" inside of it, but it doesn't, and it should bind with my Contact Class. 组合框应在其中显示“同事,家人,朋友”,但不显示,并且应与我的联系方式绑定。
http://pastebin.com/BtY7SSjR http://pastebin.com/BtY7SSjR
<ListBox x:Name="myItemsControl" Grid.Column="0" Grid.Row="1" Background="LightBlue">
<ItemsControl.ItemTemplate>
<DataTemplate x:Name="myDataTemplate">
<StackPanel>
<TextBlock Height="50" x:Name="contactName">
<Run FontSize="14" Text="{Binding Path=FirstName}"/>
<Run FontSize="14" Text="{Binding Path=LastName}"/>
</TextBlock>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ListBox>
How do I correctly bind this so it works with my Contact? 我该如何正确绑定它以便与我的联系人一起使用?
EDIT Everything works except for my Enums 编辑一切正常,除了我的枚举
All textboxes show what they are supposed to. 所有文本框均显示其应有的功能。 Only the ComboBox doesn't work because it's not binded to all of the values that the Enum can be.
只有ComboBox不起作用,因为它没有绑定到Enum可以使用的所有值。 I need to make an IEnumerable, maybe a ObservableCollection and add all of the values of the Group to it, and then bind to it.
我需要制作一个IEnumerable,也许是一个ObservableCollection,并将Group的所有值添加到它,然后绑定到它。 That works, however I have no idea how to bind my contactList(the observablecollection that's holding all of my contacts) to hold the Group collection.
那行得通,但是我不知道如何绑定我的contactList(保存我所有联系人的observablecollection)来保存Group集合。 Any ideas?
有任何想法吗?
Thanks! 谢谢!
I fixed it by doing the following: 我通过执行以下操作修复了该问题:
<ComboBox ItemsSource="{Binding ElementName=myItemsControl, Path=SelectedItem.ContactGroupValues, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
SelectedItem="{Binding ElementName=myItemsControl, Path=SelectedItem.ContactGroup, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Row="10" Grid.Column="1"/>
and then in the Contact class: 然后在Contact类中:
private Group _Group;
public Group ContactGroup
{
get
{
return _Group;
}
set
{
_Group = value;
}
}
public IEnumerable<Group> ContactGroupValues
{
get
{
return Enum.GetValues(typeof(Group)).Cast<Group>();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.