Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何获取组合框内容值?_C#_Wpf_Combobox_Win Universal App - Fatal编程技术网

C# 如何获取组合框内容值?

C# 如何获取组合框内容值?,c#,wpf,combobox,win-universal-app,C#,Wpf,Combobox,Win Universal App,我想从我的组合框中获取内容。我已经试过了一些方法,但效果不好 以下是我的组合框示例: <ComboBox x:Name="cmbSomething" Grid.Column="1" Grid.Row="5" HorizontalAlignment="Center" Margin="0 100 0 0" PlaceholderText="NothingToShow"> <ComboBoxItem>First item</ComboBoxItem>

我想从我的组合框中获取内容。我已经试过了一些方法,但效果不好

以下是我的组合框示例:

<ComboBox x:Name="cmbSomething" Grid.Column="1" Grid.Row="5" HorizontalAlignment="Center" Margin="0 100 0 0" PlaceholderText="NothingToShow">
    <ComboBoxItem>First item</ComboBoxItem>
    <ComboBoxItem>Second item</ComboBoxItem>
</ComboBox>
为什么这个代码不起作用? 我的结果不是显示组合框内容,而是显示以下文本:

Windows.UI.Xaml.Controls.ComboBoxItem


您需要
ComboBoxItem
Content
属性。所以这应该是你想要的:

var comboBoxItem = cmbSomething.Items[cmbSomething.SelectedIndex] as ComboBoxItem;
if (comboBoxItem != null)
{
    string selectedcmb = comboBoxItem.Content.ToString();
}

您需要
ComboBoxItem
Content
属性。所以这应该是你想要的:

var comboBoxItem = cmbSomething.Items[cmbSomething.SelectedIndex] as ComboBoxItem;
if (comboBoxItem != null)
{
    string selectedcmb = comboBoxItem.Content.ToString();
}

我已经扩展了我关于使用模型而不是直接UI代码隐藏访问的建议。这些是必需的部分:

BaseViewModel.cs 在我的工作项目中,我在很多视图模型中使用它。从技术上讲,您可以直接在视图模型中实现它,但我喜欢将其集中化以便重用

公共抽象类BaseViewModel:INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
私有哈希表值=新哈希表();
受保护的void SetValue(字符串名称、对象值)
{
this.values[name]=值;
OnPropertyChanged(名称);
}
受保护对象GetValue(字符串名称)
{
返回此.values[name];
}
受保护的void OnPropertyChanged(字符串名称)
{
PropertyChangedEventHandler处理程序=PropertyChanged;
if(处理程序!=null)
{
处理程序(此,新PropertyChangedEventArgs(名称));
}
}
}
ComboViewModel.cs 这是您将绑定的内容,以便于获取值。我称之为
ComboViewModel
,因为我只处理您的组合框。您需要一个更大的视图模型和更好的名称来处理所有数据绑定

public class ComboViewModel : BaseViewModel
{

  public ComboViewModel()
  {
     Index = -1;
     Value = string.Empty;
     Items = null;
  }

  public int Index
  {
     get { return (int)GetValue("Index"); }
     set { SetValue("Index", value); }
  }
  public string Value
  {
     get { return (string)GetValue("Value"); }
     set { SetValue("Value", value); }
  }
  public List<string> Items
  {
     get { return (List<string>)GetValue("Items"); }
     set { SetValue("Items",value); }
  }
}

您可以添加用于从数据库填充模型、保存对数据库的更改等的业务逻辑。当您更改视图模型的属性时,UI将自动更新。

我已经详细介绍了关于使用模型而不是直接使用access后的UI代码的建议。这些是必需的部分:

BaseViewModel.cs 在我的工作项目中,我在很多视图模型中使用它。从技术上讲,您可以直接在视图模型中实现它,但我喜欢将其集中化以便重用

公共抽象类BaseViewModel:INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
私有哈希表值=新哈希表();
受保护的void SetValue(字符串名称、对象值)
{
this.values[name]=值;
OnPropertyChanged(名称);
}
受保护对象GetValue(字符串名称)
{
返回此.values[name];
}
受保护的void OnPropertyChanged(字符串名称)
{
PropertyChangedEventHandler处理程序=PropertyChanged;
if(处理程序!=null)
{
处理程序(此,新PropertyChangedEventArgs(名称));
}
}
}
ComboViewModel.cs 这是您将绑定的内容,以便于获取值。我称之为
ComboViewModel
,因为我只处理您的组合框。您需要一个更大的视图模型和更好的名称来处理所有数据绑定

public class ComboViewModel : BaseViewModel
{

  public ComboViewModel()
  {
     Index = -1;
     Value = string.Empty;
     Items = null;
  }

  public int Index
  {
     get { return (int)GetValue("Index"); }
     set { SetValue("Index", value); }
  }
  public string Value
  {
     get { return (string)GetValue("Value"); }
     set { SetValue("Value", value); }
  }
  public List<string> Items
  {
     get { return (List<string>)GetValue("Items"); }
     set { SetValue("Items",value); }
  }
}

您可以添加用于从数据库填充模型、保存对数据库的更改等的业务逻辑。当您更改视图模型的属性时,UI将自动更新。

@ktos1234如果您想让自己更轻松,只需使用一个属性创建一个视图模型,您可以绑定到组合框以检索SelectedItem的值。@ktos1234我给出了一个答案,其中有一个关于如何使用视图模型而不是直接控制访问的快速示例。@ktos1234如果您想让自己更轻松,只需使用一个属性创建一个视图模型,您可以绑定到组合框以检索SelectedItem的值。@ktos1234我给出了一个答案,其中有一个关于如何使用视图模型而不是直接控制访问的快速示例。