我正在使用dapper使用列表从数据库中检索一些数据。我已经能够使用
使用ListBox做到这一点private void UpdateBinding()
{
displayLastNameLB.ItemsSource = people;
displayLastNameLB.DisplayMemberPath = "FullInfo";
}
,现在我想在TextBlock中显示它,但是我找不到方法。 ItemSource
和DisplayMemberPath
的等效文本块是什么?
这是完整的代码。
public partial class MainWindow : Window
{
List<Person> people = new List<Person>();
public MainWindow()
{
InitializeComponent();
UpdateBinding();
}
private void UpdateBinding()
{
displayLastNameLB.ItemsSource = people;
displayLastNameLB.DisplayMemberPath = "FullInfo";
}
private void Search_Click(object sender, RoutedEventArgs e)
{
DataAccess db = new DataAccess();
people = db.GetPeople(lastNameTB.Text);
UpdateBinding();
}
}
人员列表
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string PhoneNumber { get; set; }
public string FullInfo
{
get
{
return $"{ FirstName } { LastName } ({ PhoneNumber })";
}
}
public string FirstNameOnly
{
get
{
return $"{ FirstName }";
}
}
}
答案 0 :(得分:0)
似乎您想使用ComboBox
。在那里,您有一个选定的项目,并有一个项目源供用户选择。
ComboBox
如下所示:
<ComboBox ItemsSource="{Binding people}" DisplayMemberPath="FullInfo" IsEditable="True"/>
使用IsEditable="True"
,用户可以输入自定义值,看起来像TextBox
答案 1 :(得分:0)
只需在XAML中将ListBox
替换为TextBlock
并设置Text
属性:
public partial class MainWindow : Window
{
List<Person> people = new List<Person>();
public MainWindow()
{
InitializeComponent();
UpdateBinding();
}
private void UpdateBinding()
{
if (people != null && people.Count > 0)
{
textBlock.Text = people[0].FullInfo;
}
}
private void Search_Click(object sender, RoutedEventArgs e)
{
DataAccess db = new DataAccess();
people = db.GetPeople(lastNameTB.Text);
UpdateBinding();
}
}
答案 2 :(得分:0)
在WPF中,执行此操作的首选方法是使用DataBinding和模型以及支持INotifyPropertyChanged的视图模型。在您的示例中,我在Window.DataContext
中看不到任何对象,因此我假设它只是Window本身。这是一个如何实现此目标的代码示例:
// MainViewModel.cs
public class MainViewModel : ObservableObject
{
public MainViewModel()
{
searchCommand = new RelayCommand(SearchExecute);
People = new ObservableCollection<Person>();
}
private ObservableCollection<Person> people;
public ObservableCollection<Person> People
{
get { return people; }
protected set { Set(ref people, value); }
}
private string lastNameTB;
public string LastNameTB
{
get { return lastNameTB; }
set { Set(ref lastNameTB, value); }
}
private Person selectedPerson;
public Person SelectedPerson
{
get { return selectedPerson; }
set { Set(ref selectedPerson, value); }
}
private RelayCommand searchCommand;
public ICommand SearchCommand
{
get { return searchCommand; }
}
private void SearchExecute(object _)
{
DataAccess db = new DataAccess();
var data = db.GetPeople(LastNameTB);
var results = new ObservableCollection<Person>(data);
People = results;
SelectedPerson = results.FirstOrDefault();
}
}
// Person.cs
public class Person : ObservableObject
{
private string firstName;
public string FirstName
{
get { return firstName; }
set { Set(ref firstName, value); FullInfo = null; }
}
private string lastName;
public string LastName
{
get { return lastName; }
set { Set(ref lastName, value); FullInfo = null; }
}
private string email;
public string Email
{
get { return email; }
set { Set(ref email, value); }
}
private string phoneNumber;
public string PhoneNumber
{
get { return phoneNumber; }
set { Set(ref phoneNumber, value); FullInfo = null; }
}
private string fullInfo;
public string FullInfo
{
get { return fullInfo; }
set
{
value = $"{FirstName} {LastName} ({PhoneNumber})";
Set(ref fullInfo, value);
}
}
}
// MainWindow.xaml
<Window x:Class="YourNameSpace.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:vm="clr-namespace:YourNameSpace.ViewModels"
mc:Ignorable="d"
Title="Search"
Height="400" Width="600"
MinHeight="150" MinWidth="250">
<Window.DataContext>
<vw:MainViewModel />
</Window.DataContext>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Label Grid.Row="0" Grid.Column="0">Last Name</Label>
<TextBox Grid.Row="0" Grid.Column="1"
Text="{Binding LastNameTB,UpdateSourceTrigger=PropertyChanged}" />
<Label Grid.Row="1" Grid.Column="0">Last Name</Label>
<ListView Grid.Row="1" Grid.Column="1"
ItemsSource="{Binding People,Mode=OneWay}"
SelectedItem="{Binding SelectedPerson}">
<ListView.View>
<GridView>
<GridViewColumn Header="First Name" Width="80" DisplayMemberBinding="{Binding FirstName}" />
<GridViewColumn Header="Last Name" Width="80" DisplayMemberBinding="{Binding LastName}" />
<GridViewColumn Header="Phone Number" Width="80" DisplayMemberBinding="{Binding PhoneNumber}" />
<GridViewColumn Header="Email" Width="80" DisplayMemberBinding="{Binding Email}" />
</GridView>
</ListView.View>
</ListView>
<Grid Grid.Row="2" Grid.ColumnSpan="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Button Grid.Column="1" MinHeight="26" MinWidth="80" Command="{Binding SearchCommand}">Search</Button>
</Grid>
</Grid>
</Window>
// ObservableObject.cs (if you use an MVVM framework, there will most likely be a different base object)
public abstract class ObservableObject : INotifyPropertyChanged
{
public void RaisePropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
protected virtual bool Set<T>(ref T field, T value, bool doNotCheckEquality = false, [CallerMemberName] string propertyName = null)
{
if (!doNotCheckEquality)
{
if (EqualityComparer<T>.Default.Equals(field, value)) return false;
}
var args = new PropertyChangedEventArgs<T>(propertyName);
field = value;
RaisePropertyChanged(propertyName, args);
return true;
}
}
// RelayCommand.cs (just a simple version of RelayCommand; there are many different implementations)
public class RelayCommand : ICommand
{
private readonly Action<object> _execute;
private readonly Predicate<object> _canExecute;
public RelayCommand(Action<object> execute)
: this(execute, null)
{ }
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
throw new ArgumentNullException(nameof(execute));
_execute = execute;
_canExecute = canExecute;
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
[DebuggerStepThrough]
public virtual bool CanExecute(object parameter)
{
return _canExecute == null || _canExecute(parameter);
}
public virtual void Execute(object parameter)
{
_execute(parameter);
}
public void RaiseCanExecuteChanged()
{
CommandManager.InvalidateRequerySuggested();
}
}