簡體   English   中英

WPF枚舉綁定到ComboBox

[英]WPF Enum binding to ComboBox

我的枚舉未綁定到我的聯系人管理器。

我有一個Enum班

    [Serializable]
    public enum Group
    {
        Friend,
        Family,
        Coworker
    }
}

然后我有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;
        }

我的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}"/>

組合框應在其中顯示“同事,家人,朋友”,但不顯示,並且應與我的聯系方式綁定。

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>

我該如何正確綁定它以便與我的聯系人一起使用?

編輯一切正常,除了我的枚舉

http://puu.sh/b3JDK.jpg

所有文本框均顯示其應有的功能。 只有ComboBox不起作用,因為它沒有綁定到Enum可以使用的所有值。 我需要制作一個IEnumerable,也許是一個ObservableCollection,並將Group的所有值添加到它,然后綁定到它。 那行得通,但是我不知道如何綁定我的contactList(保存我所有聯系人的observablecollection)來保存Group集合。 有任何想法嗎?

謝謝!

我通過執行以下操作修復了該問題:

<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"/>

然后在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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM