如何将变量绑定为多种类型? LINQ [英] How to bind a variable with multiple types? LINQ
问题描述
我一直试图将变量绑定到WPF.我正在使用LINQ来获取数据.该条件是唯一的int
类型,它返回一行包含30列的数据,其中需要返回几种不同的类型.
I have been trying to bind a variable to WPF. I am using LINQ to get the data. The criteria is a unique int
type and returns a single row of data that has 30 columns, of which there are several different types that need to be returned.
I am getting this error InvalidOperationException Class, but I can't figure out how to fix this?
这是我到目前为止所拥有的,
This is what I have thus far,
internal class DatabaseQueries
{
public static IEnumerable<int> ModValues(DatabaseDataContext database, int staffNo)
{
return database.Staff_Mod_TBLs
.Where(staff => staff.Staff_No == staffNo).Cast<int>().ToList();
}
}
这是设置变量的代码,
int staffNumber = 192356;
var modTblValue = DatabaseQueries.ModValues(sql, staffNumber);
还有XAML,(由于某种原因,我无法发布所有XAML,所以这是一段简短的代码.)
And the XAML, (for some reason I can't post all the XAML, so this is an abbreviated snip of code.)
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock FontWeight="Normal" Text="{Binding Path=modTblValue, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
推荐答案
创建如下所示的属性,
private string _modTblValue;
public string modTblValue
{
get { return _modTblValue ; }
set { modTblValue = value; NotifyPropertyChanged(); }
}
将您从数据库获得的值分配给modTblValue
Assign the value you get from DataBase to modTblValue
modTblValue = DatabaseQueries.ModValues(sql, staffNumber);
使用UI绑定您的媒体资源modTblValue
Bind your property modTblValue
with UI
<TextBlock FontWeight="Normal" Text="{Binding Path=modTblValue, UpdateSourceTrigger=PropertyChanged}" />
如果您需要一个属性集合来绑定到DataGrid
,则创建一个包含Properties
的class
,并创建要作为DataGrid
的ItemSource
绑定的类列表的属性>
If you need a collection of your property to bind to a DataGrid
then create a class
which contain your Properties
, and make a property of list of your class to bind as ItemSource
of your DataGrid
这篇关于如何将变量绑定为多种类型? LINQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!