2013-05-28 134 views
0

我结合实体对象gridcontrolWPF绑定实体框架

{ 
     InitializeComponent(); 

     gridControl1.DataContext = from q in myEnt.item 
            from b in myEnt.item_type 
            where q.item_type_fk == b.item_type_id 
               select new { q.item_name, q.item,m_type}; 

    } 

其运作良好。当我单击网格控件中的一行时,我想在文本框中显示相关数据。 我怎样才能做到这一点我试过这样:

<TextBox Name="TextBox3" Text="{Binding Path=item_name}"/> 

不起作用。

回答

1

试试这个(你需要明确的告诉文本框在哪里可以找到item_name):

<TextBox Name="TextBox3" 
     Text="{Binding ElementName=gridControl1, Path=SelectedItem.item_name}"/> 

编辑:

gridControl1似乎是一个DevExpress的GridControl不具有SelectedItem属性(?)。根据这一支持文章Change selected item through databinding,这种结合可能会工作,而不是(TableView.FocusedRow):

<TextBox Name="TextBox3" 
     Text="{Binding ElementName=tableView1, Path=FocusedRow.item_name}" /> 
+0

它不工作。 我必须参考实体对象。如何显示未包含在网格中的字段? – user1065131

+0

你是什么意思? 'item_name'是网格中的一个字段,不是吗?如果在TextBox中看不到任何内容,则可以在Visual Studio中检查“输出”窗口,并在运行项目时搜索绑定错误。另外,如果你在这里发布更多的XAML,可能会更容易看到发生了什么。 – Sphinxxx

+0

当然“gridControl1”必须是DataGrid的名称。请发布更多的代码用户。 –

0
<dxr:DXRibbonWindow 
x:Class="Eszkoz.MainWindow" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:local="clr-namespace:Eszkoz" 
xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" 
xmlns:dxr="http://schemas.devexpress.com/winfx/2008/xaml/ribbon" 
xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars" 
xmlns:dxbh="http://schemas.devexpress.com/winfx/2008/xaml/bars/internal" 
xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors" 
xmlns:dxd="http://schemas.devexpress.com/winfx/2008/xaml/docking" 
Title="DXApplication" Height="700" Width="1100" 
SnapsToDevicePixels="True" UseLayoutRounding="True" 
dx:ThemeManager.ThemeName="Office2013" xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"> 
<dxr:DXRibbonWindow.Resources> 
    <dx:EntitySimpleDataSource x:Key="EntitySimpleDataSource" ContextType="local:eszkozEntities" dx:DesignDataManager.DesignData="{dx:DesignDataSettings RowCount=5, UseDistinctValues=True}" Path="eszkoz" /> 
</dxr:DXRibbonWindow.Resources> 

<dxb:BarManager x:Name="barManager" dxbh:BlendHelperForBarsAndRibbon.IsDesignControl="true"> 
    <DockPanel> 
     <dxd:DockLayoutManager x:Name="dockLayoutManager"> 
      <dxd:LayoutGroup> 
       <dxd:LayoutPanel ItemWidth="200" Caption="Navigation" Padding="1"> 
        <dxg:GridControl AutoPopulateColumns="True" Name="gridControl1" ItemsSource="{Binding}"> 
         <dxg:GridControl.View> 
          <dxg:TableView Name="tableView1" ShowTotalSummary="True" /> 
         </dxg:GridControl.View> 
        </dxg:GridControl> 
       </dxd:LayoutPanel> 
       <dxd:LayoutGroup Orientation="Vertical" ItemWidth="4*"> 
        <dxd:LayoutPanel Caption="MainView" ItemHeight="3*"> 
         <dxd:LayoutGroup Orientation="Vertical"> 
          <dxd:LayoutControlItem Caption="Layout Item"> 
           <TextBox Height="23" Name="TextBox3" Width="100" Margin="2" HorizontalAlignment="Left" Text="{Binding ElementName=gridControl1, Path=SelectedItem.item_name}"/> 
          </dxd:LayoutControlItem> 
         </dxd:LayoutGroup> 
        </dxd:LayoutPanel> 
        <dxd:LayoutPanel Caption="DetailView" ItemHeight="2*"></dxd:LayoutPanel> 
       </dxd:LayoutGroup> 
      </dxd:LayoutGroup> 
     </dxd:DockLayoutManager> 
    </DockPanel> 
</dxb:BarManager> 

+0

我自己并没有使用DevExpress的组件,但我已经用一个建议更新了我的答案。 – Sphinxxx

+0

我必须参考实体对象。如何显示未包含在网格中的字段? – user1065131

+0

然后我会将完整的实体对象添加到网格中,但只为网格中显示的内容创建列(我假设您可以设置“AutoPopulateColumns = false”,然后DevExpress有一些方法可以让您自己创建一组列)。这样,您就可以通过“TableView.FocusedRow”访问所选的实体对象。 – Sphinxxx