如何将变量绑定为多种类型? LINQ [英] How to bind a variable with multiple types? LINQ

查看:61
本文介绍了如何将变量绑定为多种类型? 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,则创建一个包含Propertiesclass,并创建要作为DataGridItemSource绑定的类列表的属性>

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆