如何在asp.net的下拉列表中获取选定的值

时间:2013-12-27 06:42:49

标签: c# asp.net wpf

我在关闭它后在下拉列表中获取所选值时遇到问题。 设计文件:

  <StackPanel Orientation="Horizontal" Margin="2">
                    <TextBlock Width="60" VerticalAlignment="Center" Text="state"></TextBlock>
                    <ComboBox  Name="cmbState" Margin="{StaticResource DefaultMargin}" Height="20" Width="220" ItemsSource="{Binding state}" DropDownOpened="cmbState_OnDropDownOpened" DropDownClosed="cmbState_OnDropDownClosed">
                        <ComboBox.ItemTemplate>
                            <DataTemplate>
                                <CheckBox Content="{Binding name}" IsChecked="{Binding selected, Mode=TwoWay}"/>
                            </DataTemplate>
                        </ComboBox.ItemTemplate>
                    </ComboBox>
                </StackPanel>


 public class State : ViewModelBase, ICloneable<State>
    {
        public State()
        {
            Selected = false;
        }

        public string name { get; set; }

        private bool Selected;
        public bool selected
        {
            get { return Selected; }
            set
            {
                if (value == Selected) return;
                Selected = value;
                NotifyPropertyChanged("selected");
            }
        }

        public State clone()
        {
            return MemberwiseClone() as State;
        }
    }
List<string> allState = new List<string> { "Proposed", "Active", "Resolved", "Closed" };
var states = allState.Select(state => new State { name = state }).ToList();
cmbState.ItemsSource = states;

private void cmbState_OnDropDownOpened(object sender, EventArgs e)
        {
            var selectedState = new List<string>();
            cmbState.Items.ToList().ForEach(item =>
            {
                var version = item as State;
                if (version == null) return;
                if (version.selected)
                {
                    if (!selectedState.Contains(version.name))
                        selectedState.Add(version.name);
                }
            });
            if (selectedState.Any())
            {

                var states = cmbState.ItemsSource as List<State>;
                if (states == null) return;
                var release = new State { name = string.Join(";", selectedState), selected = true };
                states.Insert(0, release);
                cmbState.SelectedIndex = 0;
            }
        }
        private void cmbState_OnDropDownClosed(object sender, EventArgs e)
        {
            var selectedState = cmbIssueType.Items[0] as State;

            if (selectedState == null) return;

            var states = cmbIssueType.ItemsSource as List<State>;

            if (states == null) return;

            foreach (var item in selectedState.name.Split(';').Select(version => states.FirstOrDefault(v => v.name == version)).Where(item => item != null))
          {
                item.selected = true;
            }
            states.Remove(selectedState);
        }

我已经复制粘贴了我的整个代码,任何人都可以请告诉我如何获得所选的下拉列表值,我在第二次下载期间获得所选值,我希望在下拉关闭时相同。有什么帮助吗?

由于

2 个答案:

答案 0 :(得分:0)

对不起,我自己修好了。问题是我改变了事件名称。我错误地在开放和打开事件代码中写了关闭事件代码。 : - )

答案 1 :(得分:0)

以下是答案

@Html.DropDownList("Gender", 
  new SelectList(
    Enum.GetValues(
      typeof(MvcLahore1.Models.Gender)
    ),
    selectedValue:Model.Gender), 
    new { @class = "form-control"}
  )