Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.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# 将combobox项源绑定到这些对象的特定属性_C#_Wpf_Mvvm_Mvvm Light - Fatal编程技术网

C# 将combobox项源绑定到这些对象的特定属性

C# 将combobox项源绑定到这些对象的特定属性,c#,wpf,mvvm,mvvm-light,C#,Wpf,Mvvm,Mvvm Light,假设我有一个包含名称和id属性的TeamParameter对象列表。我想要一个组合框,它将显示TeamParameter对象的列表,但只向用户显示组合框中每个对象的name属性。有没有办法在MainWindow.xaml中绑定到该属性 尝试了点符号,认为它可以工作,但没有 MainViewModel.cs public class MainViewModel : ViewModelBase { private List<TeamParameters> _teams;

假设我有一个包含名称和id属性的TeamParameter对象列表。我想要一个组合框,它将显示TeamParameter对象的列表,但只向用户显示组合框中每个对象的name属性。有没有办法在MainWindow.xaml中绑定到该属性

尝试了点符号,认为它可以工作,但没有

MainViewModel.cs

public class MainViewModel : ViewModelBase
{
        private List<TeamParameters> _teams;

        public class TeamParameters
        {
            public string Name { get; set; }

            public int Id { get; set; }
        }

        public List<TeamParameters> Teams
        {
            get { return _teams; }
            set { Set(ref _teams, value); }
        }
}
public类MainViewModel:ViewModelBase
{
私人名单小组;
公共类参数
{
公共字符串名称{get;set;}
公共int Id{get;set;}
}
公开名单小组
{
获取{return\u teams;}
集合{set(ref _,value);}
}
}
MainWindow.xaml

<Window x:Class="LiveGameApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:LiveGameApp"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800"
        DataContext="{Binding Main, Source={StaticResource Locator}}">



    <DockPanel>
        <ComboBox  Name="TeamChoices" ItemsSource="{Binding Team.Name}"  DockPanel.Dock="Top" Height="30" Width="175" VerticalContentAlignment="Center" HorizontalContentAlignment="Center"></ComboBox>
    </DockPanel>
</Window>

要指向数据模型上的特定属性,您可以通过设置
DisplayMemberPath
来指定成员路径:

<ComboBox  ItemsSource="{Binding Teams}" DisplayMemberPath="Name" />
XAML


将ItemSource绑定更改为ItemsSource=“{binding Teams}”,并提供组合框的DisplayMemberPath属性作为名称,如DisplayMemvberPath=“Name”谢谢,现在一切都好了!我没有想到要重写ToString(),但这为我以后打开了一个有趣的选择。现在我使用了显示成员路径选项,一切都很好。谢谢
public class TeamParameters
{
  public override string ToString() => this.Name;

  public string Name { get; set; }

  public int Id { get; set; }
}
<ComboBox  ItemsSource="{Binding Teams}" />
<ComboBox ItemsSource="{Binding Teams}">
    <ComboBox.ItemTemplate>
        <DataTemplate DataType="TeamParameters">
            <TextBlock Text="{Binding Name}" /> 
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>