我正在使用 C# WPF 連接到 SQL Server 中的資料庫
我有一個組合框,它有兩列要顯示一個 SelectedValuePath
問題是我不能將 LINQ 用于其中一列(屬性)
我的 XAML:
<Grid Background="#FFCFCFCF">
<ComboBox x:Name="HESNAGHD" IsEditable="True" IsTextSearchEnabled="True" FlowDirection="RightToLeft" Margin="148,73,149,314" RenderTransformOrigin="0.5,0.5" StaysOpenOnEdit="True" >
<ComboBox.ItemsPanel >
<ItemsPanelTemplate>
<VirtualizingStackPanel VirtualizingPanel.IsVirtualizing="True" VirtualizingPanel.VirtualizationMode="Recycling"/>
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding nam}" TextAlignment="Justify" Width="500" ></TextBlock>
<TextBlock Text="{Binding Expr1}" TextAlignment="Justify" Width="100" ></TextBlock>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<Label Content="Mine ComboBox : " HorizontalAlignment="Left" Height="28" Margin="34,77,0,0" VerticalAlignment="Top" Width="109"/>
<Label Content="Search : " HorizontalAlignment="Left" Height="28" Margin="30,10,0,0" VerticalAlignment="Top" Width="72"/>
<TextBox x:Name="OneSearch" HorizontalAlignment="Left" Height="32" Margin="148,10,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="359" TextChanged="OneSearch_TextChanged"/>
</Grid>
我的客服:
public partial class CUST_HESAB
{
public string nam { get; set; }
public string Expr1 { get; set; }
public override string ToString() => nam;
}
public partial class MainWindow : Window
{
bool IsReadyAll = false;
List<CUST_HESAB> PBLIST = new List<CUST_HESAB>();
CorrectWPFEntities dbms = new CorrectWPFEntities();
public MainWindow()
{
InitializeComponent();
var ITT = dbms.Database.SqlQuery<CUST_HESAB>("SELECT hes, NAME AS nam, hes AS Expr1 FROM CUST_HESAB").ToList();
foreach (var item in ITT)
{
PBLIST.Add(item);
}
HESNAGHD.ItemsSource = PBLIST.ToList();
HESNAGHD.SelectedValuePath = "Expr1";
HESNAGHD.SelectedIndex = 0;
HESNAGHD.IsDropDownOpen = true;
HESNAGHD.IsDropDownOpen = false;
}
private void OneSearch_TextChanged(object sender, TextChangedEventArgs e)
{
if (IsReadyAll == true)
{
if (!string.IsNullOrEmpty(OneSearch.Text.Trim().ToLower()))
{
HESNAGHD.IsDropDownOpen = true;
var myfilter0 = from u in PBLIST where u.nam.Contains(OneSearch.Text) select u.nam.ToList();
var myfilter = PBLIST.Where(x => x.nam.Contains(OneSearch.Text)).ToList();
if (!ReferenceEquals(myfilter, null))
{
HESNAGHD.ItemsSource = myfilter;
}
else
{
HESNAGHD.ItemsSource = dbms.Database.SqlQuery<CUST_HESAB>("SELECT hes, NAME AS nam, hes AS Expr1 FROM CUST_HESAB").ToList();
}
}
else
{
HESNAGHD.ItemsSource = dbms.Database.SqlQuery<CUST_HESAB>("SELECT hes, NAME AS nam, hes AS Expr1 FROM CUST_HESAB").ToList();
}
}
}
private void Window_ContentRendered(object sender, EventArgs e)
{
IsReadyAll = true;
}
}
我的問題:在 OneSearch_TextChanged The Where is not working 中,它說 null 但不是!
在此處輸入影像描述
在此處輸入影像描述
請幫忙
uj5u.com熱心網友回復:
更改此行
var myfilter = PBLIST.Where(x => x.nam.Contains(OneSearch.Text)).ToList();
到
var myfilter = PBLIST.Where(x => x.nam != null && x.nam.Contains(OneSearch.Text)).ToList();
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/419179.html
標籤:
上一篇:為專案串列賦值
