可编辑的组合框不获取项目WPF

时间:2018-10-29 19:40:35

标签: c# wpf combobox

我有一个设置为isEditable = true的组合框,但是当我从键盘上按下向下箭头时,会收到此消息

  

MoalemYar.DataClass.DataTransferObjects + StudentsDto

这是我的组合框,而不是我选择的项目

<ComboBox
    x:Name="cmbEditStudent"
    IsEditable="True"
    SelectedValue="{Binding LName}"
    SelectedValuePath="Id">
    <ComboBox.ItemTemplate>
        <DataTemplate>
                <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Name}" />
                <TextBlock Text=" " />
                <TextBlock Text="{Binding LName}" />
                <TextBlock Text=" - " />
                <TextBlock Text="نام پدر(" />
                <TextBlock Text="{Binding FName}" />
                <TextBlock Text=")" />
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

2 个答案:

答案 0 :(得分:3)

Try this hope it will helps you
    <ComboBox Height="25" Margin="80,12,12,0" Name="comboBox1" VerticalAlignment="Top" 
              ItemsSource="{Binding StudentList}" IsEditable="True" TextSearch.TextPath="StudentName">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding StudentName}" />
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

答案 1 :(得分:2)

默认情况下,组合框中的第一项是由向下箭头选择的,然后可以按向上/向下箭头键相应地更改所选的项目。

尝试以下代码。

在XAML中

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="20"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <ComboBox
        x:Name="cmbEditStudent"
        IsTextSearchEnabled="True"   
        TextSearch.TextPath = "Name"
        IsEditable="True"
        ItemsSource="{Binding StudentsList}"
        SelectedItem="{Binding SelectedStudent}">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Name}" />
                    <TextBlock Text=" " />
                    <TextBlock Text="{Binding LName}" />
                    <TextBlock Text=" - " />
                    <TextBlock Text="نام پدر(" />
                    <TextBlock Text="{Binding FName}" />
                    <TextBlock Text=")" />
                </StackPanel>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

    <TextBox Width="200" Height="20" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>

在其视图模型中

public partial class MainWindow : Window, INotifyPropertyChanged
{
    private ObservableCollection<Student> studentsList = new ObservableCollection<Student>();
    public ObservableCollection<Student> StudentsList
    {
        get { return studentsList; }
        set
        {
            studentsList = value;
            NotifyPropertyChanged();
        }
    }

    private Student selectedStudent;
    public Student SelectedStudent
    {
        get { return selectedStudent; }
        set
        {
            selectedStudent = value;
        }
    }

    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;

        StudentsList.Add(new Student("Paul", "LName1", "FName1"));
        StudentsList.Add(new Student("Alex", "LName2", "FName2"));
        StudentsList.Add(new Student("Steve", "LName3", "FName3"));
    }

    #region Notify Property
    public event PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged([CallerMemberName] string propName = null)
    {
        if (!string.IsNullOrWhiteSpace(propName))
        {
            Application.Current.Dispatcher.Invoke(new Action(() =>
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
            }));
        }
    }

    #endregion
}

学生班

 public class Student : NotifiableBase
{
    private string name;
    public string Name
    {
        get { return name; }
        set
        {
            name = value;
            NotifyPropertyChanged();
        }
    }

    private string lName;
    public string LName
    {
        get { return lName; }
        set
        {
            lName = value;
            NotifyPropertyChanged();
        }
    }

    private string fName;
    public string FName
    {
        get { return fName; }
        set
        {
            fName = value;
            NotifyPropertyChanged();
        }
    }

    public Student(string name, string lName, string fName)
    {
        this.name = name;
        this.lName = lName;
        this.fName = fName;
    }
}

NotifiableBase

public class NotifiableBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged([CallerMemberName] string propName = null)
    {
        if (!string.IsNullOrWhiteSpace(propName))
        {
            Application.Current.Dispatcher.Invoke(new Action(() =>
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
            }));
        }
    }
}