Редактируемый список не получает предмет wpf - PullRequest
0 голосов
/ 29 октября 2018

У меня есть поле со списком, которое я установил 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 голосов
/ 30 октября 2018

По умолчанию первый элемент в поле со списком выбирается стрелкой вниз, затем вы можете нажимать клавиши со стрелками вверх / вниз, чтобы соответствующим образом изменить выбранный элемент.

Попробуйте следующий код.

В 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));
            }));
        }
    }
}
0 голосов
/ 30 октября 2018
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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...