如何解决如何处理在文本框中输入的多个数据类型值以进行搜索
下面是Xaml代码,其中有一个组合框,其中包含数据库列名(硬编码)。接下来,有一个文本框,它将在该ComboBox Selected类型值下获取要搜索的值。 Xaml代码
<StackPanel Orientation="Horizontal">
<Label Content="Search By" Width="100"/>
<ComboBox Name="CmbSearch" Width="100" ItemsSource="{Binding ColNames}" SelectedValue="{Binding SearchBy}"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="Enter the Value" Width="100"/>
<TextBox Name="T" Style="{StaticResource Txt}" Text="{Binding Searching,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"/>
</StackPanel>
<DataGrid Name="DG" ItemsSource="{Binding customer}" SelectedItem="{Binding SelectedCustomer}" AutoGenerateColumns="False" CanUserAddRows="False"
Height="310" Width="310" Margin="10" HorizontalAlignment="Right" CanUserDeleteRows="True">
<DataGrid.Columns>
<DataGridTextColumn Header="Id" Binding="{Binding Path='Id',UpdateSourceTrigger=PropertyChanged}" IsReadOnly="True"/>
<DataGridTextColumn Header="Name" Binding="{Binding Path='Name',UpdateSourceTrigger=PropertyChanged}" IsReadOnly="True" />
<DataGridTextColumn Header="Age" Binding="{Binding Path='Age',UpdateSourceTrigger=PropertyChanged}" IsReadOnly="True" />
<DataGridTextColumn Header="Country" Binding="{Binding Path='Country',UpdateSourceTrigger=PropertyChanged}" IsReadOnly="True" />
<DataGridCheckBoxColumn Header="Active" Binding="{Binding Path='Active',Converter={StaticResource convert}}" IsReadOnly="True"/>
<DataGridTemplateColumn Header="Delete Record" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate >
<DataTemplate>
<Button Command="{Binding DataContext.CmdDeleteUser,RelativeSource={RelativeSource AncestorType={x:Type Window}}}" CommandParameter="{Binding}" Width="60" Margin="2" Content="Delete" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
private string _SearchBy;
public string SearchBy
{
get { return _SearchBy; }
set { _SearchBy = value;
OnPropertyChange("SearchBy");
}
}
private string _Searching;
public string Searching
{
get { return _Searching; }
set
{
_Searching = value;
OnPropertyChange("Searching");
if (Searching != null)
{
collectionView.Filter = new Predicate<Object>(Search);
}
}
}
private bool Search(Object obj)
{
Customer cus = (Customer)obj;
if((SearchBy.Equals("ID")))
{
if (cus.Id.Equals(Convert.ToInt32(Searching)))
{
return true;
}
else
{
return false;
}
}
else if(SearchBy.Equals("Name"))
{
if (cus.Name.Contains(Searching))
{
return true;
}
else
{
return false;
}
}
else if(SearchBy.Equals("Age"))
{
if (cus.Age.Equals(Convert.ToInt32(Searching)))
{
return true;
}
else
{
return false;
}
}
else if(SearchBy.Equals("Country"))
{
if (cus.Country.Contains(Searching))
{
return true;
}
else
{
return false;
}
}
else if(SearchBy.Equals("Active"))
{
if (cus.Active.Equals(Convert.ToBoolean(Searching)))
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
“搜索姓名和国家/地区”工作正常,但Id,Age和Active遇到类型问题。我还为Active(这是一个组合框)创建了一个IValueconverter。对于其他类型我该怎么办?
解决方法
您可以使用int.TryParse
方法来尝试解析字符串值,然后调用Equals
。
您应该使用switch
语句。然后,您的Search
方法可以实现如下:
private bool Search(Object obj)
{
Customer cus = (Customer)obj;
switch (_SearchBy)
{
case "ID":
return int.TryParse(_Searching,out int id) && cus.Id.Equals(id);
case "Name":
return cus.Name.Contains(Searching);
case "Age":
return int.TryParse(_Searching,out int age) && cus.Age.Equals(age);
case "Country":
return cus.Country.Contains(Searching);
}
return false;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。