单击 ListView 事件后 searchBar 不更改占位符 listview_ItemSelected
searchBar not change the Placeholder after clicked on ListView event listview_ItemSelected
我在我的 MainPage 上设置了一个 searchBar 和 ListView:
<StackLayout>
<SearchBar
BackgroundColor="#d3d3d3"
x:Name="searchBar"
Placeholder="Enter city"
PlaceholderColor="#919191"
TextChanged="SearchBar_TextChanged"
SearchButtonPressed="Btn_Search">
</SearchBar>
<Frame CornerRadius="15"
OutlineColor="#919191"
Padding="2"
Margin="3">
<Label BackgroundColor="#dedede"
TextColor="#919191"
Font="Bold"
FontSize="20"
Text="Списък с населени места"
YAlign="Center"
XAlign="Center"
LineBreakMode="TailTruncation" />
</Frame>
<ListView
BackgroundColor="#d3d3d3"
ItemTapped="ListView_ItemTapped"
ItemSelected="listView_ItemSelected"
HeightRequest="88"
x:Name="listView" >
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding Place}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
之后我创建了 class 联系人,字符串为“Place” -> get;设置;
public class Contacts
{
public string Place { get; set; }
}
然后打开数据库连接以执行一个查询并像这样填写我的列表:
IEnumerable<Contacts> GetContacts(string searchText = null)
{
server = "ip-to-db";
database = "db-name";
uid = "user";
password = "pass";
string connectionString;
connectionString = "SERVER=" + server + ";" + "DATABASE=" +
database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
connection = new MySqlConnection(connectionString);
connection.Open();
var cmd = new MySqlCommand();
cmd.Connection = connection;
//save from database data
var contacts = new List<Contacts>();
//cut first four strings from selected query
MySqlCommand command = new MySqlCommand($"SELECT SUBSTRING(place, 4, 35), code AS ExtractString FROM places ORDER BY place", connection);
using (MySqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
var place = new Contacts
{
Place = reader[0].ToString() + " - " + reader[1].ToString()
};
contacts.Add(place);
}
connection.Close();
}
if (string.IsNullOrEmpty(searchText))
return contacts;
return contacts.Where(p => p.Place.StartsWith(searchText));
}
private void ListView_Refreshing(object sender, EventArgs e)
{
listView.ItemsSource = GetContacts();
listView.EndRefresh();
}
void SearchBar_TextChanged(System.Object sender, Xamarin.Forms.TextChangedEventArgs e)
{
listView.ItemsSource = data.Where(p => p.Place.StartsWith(e.NewTextValue));
}
现在我想在单击某些 ListView 元素时更改 searchBar 上的占位符,但是当我调试此文本下的最后一个代码时,我看到了来自 listView.SelectedItem.ToString();
的字符串,但是当我想要显示我在显示屏上没有看到带有此代码的字符串:
void listView_ItemSelected(System.Object sender, Xamarin.Forms.SelectedItemChangedEventArgs e)
{
LabelClick.Text = listView.SelectedItem.ToString();
searchBar.Placeholder = LabelClick.Text;
}
这是当我单击某个 listView 元素时搜索栏上准确显示的屏幕截图。
searchBar after clicked on ListView
ToString
的默认行为是 return class 的名称。这正是正在发生的事情。如果你想显示 Place
属性 你需要指定
searchBar.Placeholder = ((Contacts)listView.SelectedItem).Place;
我在我的 MainPage 上设置了一个 searchBar 和 ListView:
<StackLayout>
<SearchBar
BackgroundColor="#d3d3d3"
x:Name="searchBar"
Placeholder="Enter city"
PlaceholderColor="#919191"
TextChanged="SearchBar_TextChanged"
SearchButtonPressed="Btn_Search">
</SearchBar>
<Frame CornerRadius="15"
OutlineColor="#919191"
Padding="2"
Margin="3">
<Label BackgroundColor="#dedede"
TextColor="#919191"
Font="Bold"
FontSize="20"
Text="Списък с населени места"
YAlign="Center"
XAlign="Center"
LineBreakMode="TailTruncation" />
</Frame>
<ListView
BackgroundColor="#d3d3d3"
ItemTapped="ListView_ItemTapped"
ItemSelected="listView_ItemSelected"
HeightRequest="88"
x:Name="listView" >
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding Place}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
之后我创建了 class 联系人,字符串为“Place” -> get;设置;
public class Contacts
{
public string Place { get; set; }
}
然后打开数据库连接以执行一个查询并像这样填写我的列表:
IEnumerable<Contacts> GetContacts(string searchText = null)
{
server = "ip-to-db";
database = "db-name";
uid = "user";
password = "pass";
string connectionString;
connectionString = "SERVER=" + server + ";" + "DATABASE=" +
database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
connection = new MySqlConnection(connectionString);
connection.Open();
var cmd = new MySqlCommand();
cmd.Connection = connection;
//save from database data
var contacts = new List<Contacts>();
//cut first four strings from selected query
MySqlCommand command = new MySqlCommand($"SELECT SUBSTRING(place, 4, 35), code AS ExtractString FROM places ORDER BY place", connection);
using (MySqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
var place = new Contacts
{
Place = reader[0].ToString() + " - " + reader[1].ToString()
};
contacts.Add(place);
}
connection.Close();
}
if (string.IsNullOrEmpty(searchText))
return contacts;
return contacts.Where(p => p.Place.StartsWith(searchText));
}
private void ListView_Refreshing(object sender, EventArgs e)
{
listView.ItemsSource = GetContacts();
listView.EndRefresh();
}
void SearchBar_TextChanged(System.Object sender, Xamarin.Forms.TextChangedEventArgs e)
{
listView.ItemsSource = data.Where(p => p.Place.StartsWith(e.NewTextValue));
}
现在我想在单击某些 ListView 元素时更改 searchBar 上的占位符,但是当我调试此文本下的最后一个代码时,我看到了来自 listView.SelectedItem.ToString();
的字符串,但是当我想要显示我在显示屏上没有看到带有此代码的字符串:
void listView_ItemSelected(System.Object sender, Xamarin.Forms.SelectedItemChangedEventArgs e)
{
LabelClick.Text = listView.SelectedItem.ToString();
searchBar.Placeholder = LabelClick.Text;
}
这是当我单击某个 listView 元素时搜索栏上准确显示的屏幕截图。 searchBar after clicked on ListView
ToString
的默认行为是 return class 的名称。这正是正在发生的事情。如果你想显示 Place
属性 你需要指定
searchBar.Placeholder = ((Contacts)listView.SelectedItem).Place;