带有DisplayMemberPath的C#WPF ComboBox ItemTemplate

问题描述 投票:-2回答:1

我想设计一个带有自定义项目的组合框(每个项目都属于人员列表。]

问题是,当选择一个项目时,将显示“人”模型的类名。

我希望在选择项目时显示此人的名字。

public class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Surname { get; set; }
        public int Age { get; set; }

        public string NameAndSurname
        {
            get
            {
                return $"{Name} {Surname}";
            }
        }
    }
<ComboBox x:Name="ComboBox" Style="{StaticResource MaterialDesignFloatingHintComboBox}" materialDesign:HintAssist.Hint="Serach" IsEditable="True">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock FontWeight="Bold" Text="{Binding NameAndSurname}"/>
                <TextBlock FontWeight="Bold" Text="{Binding Age}"/>
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>
c# wpf combobox material-design-in-xaml
1个回答
0
投票

这似乎有点不完整。您缺少Combobox本身的ItemsSource,DataContext,并且您的视图模型需要包含一个集合。这是我刚刚使用Visual Studio 2019输入并检查的实现:

对于MainWindow.xaml

<Window x:Class="WpfAppForSEQuestion.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfAppForSEQuestion"
        DataContext="{Binding Source={x:Static local:ViewModel.The}}"
        Title="MainWindow" Height="450" Width="800">

    <Grid>
        <ComboBox HorizontalAlignment="Center" ItemsSource="{Binding Persons}" VerticalAlignment="Center">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock FontWeight="Bold" Text="{Binding NameAndSurname}"/>
                        <TextBlock FontWeight="Bold" Margin="10,0,0,0" Text="{Binding Age}"/>
                    </StackPanel>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
    </Grid>
</Window>

这是视图模型的一个(简单的)示例:

using System.Collections.ObjectModel;

namespace WpfAppForSEQuestion
{
    public class Person
    {
        public Person( string name, string surname, int age )
        {
            Id = 93;
            Name = name;
            Surname = surname;
            Age = age;
        }

        public int Id { get; set; }
        public string Name { get; set; }
        public string Surname { get; set; }
        public int Age { get; set; }

        public string NameAndSurname
        {
            get
            {
                return $"{Name} {Surname}";
            }
        }

   public override string ToString()
    {
        return NameAndSurname;
    }
     }

    public class ViewModel
    {
        public ViewModel()
        {
            Persons = new ObservableCollection<Person>();
            Persons.Add( new Person( "James", "Hurst", 60 ) );
            Persons.Add( new Person( "Virginia", "Hurst", 90 ) );
            Persons.Add( new Person( "Oscar", "Hurst", 100 ) );
        }

        /// <summary>
        /// Get the (singleton) instance of this view-model.
        /// </summary>
        public static ViewModel The
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new ViewModel();
                }
                return _instance;
            }
        }
        /// <summary>
        /// This is the one, singleton instance of this class.
        /// </summary>
        private static ViewModel _instance;

        public ObservableCollection<Person> Persons { get; set; }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.