2017-09-01 199 views
0

我试着去绑定/从我的主窗口 MainForm中链接一个DataGrid:绑定用户控件

<dx:DXWindow 
x:Class="LicenceManagerWPF.Forms.frmCustomerLicense" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" 
dx:ThemeManager.ThemeName="Office2016" 
xmlns:ctr="clr-namespace:LicenceManagerWPF.Controls" 
Title="CustomerLicence" Height="800" Width="1000" 
WindowStartupLocation="CenterScreen" Loaded="DXWindow_Loaded"> 
<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="50"/> 
     <RowDefinition MinHeight="200" Height="200*"/> 
     <RowDefinition Height="200*"/> 
     <RowDefinition MinHeight="25" Height="25"/> 
    </Grid.RowDefinitions> 
    <StackPanel x:Name="Stack_Top" Orientation="Horizontal" Grid.Row="0" > 
     <dx:SimpleButton x:Name="btnRefresh" Style="{StaticResource ResourceKey=BtnSmall}" ToolTip="Refresh Licenses" Glyph="{dx:DXImage Image=Refresh_32x32.png}" Content="Resfresh" /> 
     <dx:SimpleButton x:Name="btndNew" Style="{StaticResource ResourceKey=BtnSmall}" ToolTip="New License" Glyph="{dx:DXImage Image=New_32x32.png}" Content="New Customer" /> 
     <dx:SimpleButton x:Name="btnDelete" Style="{StaticResource ResourceKey=BtnSmall}" ToolTip="Delete Licence" Content="Delete" Glyph="{dx:DXImage Image=Cancel_32x32.png}"/> 
     <dx:SimpleButton x:Name="btnEdit" Style="{StaticResource ResourceKey=BtnSmall}" ToolTip="Edit Customer" Glyph="{dx:DXImage Image=EditContact_32x32.png}" /> 
     <TextBlock Text="Customer: " FontSize="20" Margin="5"/> 
     <TextBlock Text="{Binding Customer.Name}" FontSize="20"/> 
    </StackPanel> 
    <ctr:Licences TblLicenses ="{Binding LicensesTable}" Grid.Row="1"> 
    </ctr:Licences> 
    <Grid Grid.Row="2"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="auto"/> 
      <ColumnDefinition /> 
     </Grid.ColumnDefinitions>    
     <ctr:LicenseDetail x:Name="ct_LicenseDetail" Grid.Column="0"/> 
     <ctr:LicenceLog x:Name="ct_LicenseLog" Grid.Column="1"/> 
    </Grid> 
</Grid> 

我创造了这个控制:

 <UserControl x:Class="LicenceManagerWPF.Controls.Licences" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     mc:Ignorable="d" 
     d:DesignHeight="300" d:DesignWidth="500" 
     x:Name="ctrl_Licenses"> 
<Grid>   
    <Grid.RowDefinitions> 
     <RowDefinition Height="70" /> 
     <RowDefinition Height="*" /> 
    </Grid.RowDefinitions> 
    <TextBlock Text="Licence List" Grid.Row="0" FontSize="22" Margin="10" TextAlignment="Left" VerticalAlignment="Center" /> 
    <dxg:GridControl x:Name="grdLicences" ItemsSource="{Binding Path=TblLicenses,ElementName=ctrl_Licenses}" Grid.Row="1"/> 
</Grid> 

我在主窗口中的网格的一行中分配了控件。 主窗口链接(数据上下文)具有此方法

private DataTable GetLicensesTable() 
    { 
     var dt = new DataTable(); 
     dt.Columns.AddRange(new DataColumn []{ 
      new DataColumn("SerialNumber",typeof(string)), 
      new DataColumn("Product",typeof(string)), 
      new DataColumn("Status",typeof(string)), 
      new DataColumn("ActivationMode",typeof(string)), 
      new DataColumn("MaxComputers", typeof(int)), 
      new DataColumn("NumActive",typeof(int)), 
      new DataColumn("Description",typeof(string)) 
     }); 
     _Licenses.ForEach((x) => { 
      var rw = dt.NewRow(); 
      rw["SerialNumber"] = x.SerialNumber; 
      rw["Product"] = x.Product.Name; 
      rw["Status"] = x.Status; 
      rw["ActivationMode"] = Enum.GetName(typeof(ActivationModeEnum), x.ActivationMode); //x.ActivationMode; 
      rw["MaxComputers"] = x.MaxComputers; 
      rw["NumActive"] = Activated(x.Product.ProductId); 
      rw["Description"] = x.Description; 
      dt.Rows.Add(rw); 
     }); 
     return dt; 
    }   

    public DataTable LicensesTable{ 
     get { return GetLicensesTable(); } 
    } 

我想是在是在用户控件的网格显示表一类。 它是可行的吗?

我在代码tryed这是我主窗口的后面:

private void DXWindow_Loaded(object sender, RoutedEventArgs e) 
    { 
     if (_CustomerLicense != null) 
     { 
      this.DataContext = _CustomerLicense; 
      this.ct_LicensesList.grdLicences.ItemsSource = _CustomerLicense.LicensesTable(); 
     } 
     else 
     { 
      this.DataContext = _CustomerLicense.LicensesTable(); 
     } 
    } 

它说,tblLicenses不reconized或不入店。

Runnin的代码部分behin它的工作原理,但我认为它不正确的方式,即时通讯使用控制。

问候

+0

这很难理解你想要做什么。您需要在UC中附加或依赖财产,(对于您来说第二个会更好)。当你创建它时,你可以将它分配给你的“tblLicences”,然后绑定你想要的。 而第二件事:为什么你写了: else this.DataContext = _CustomerLicense.LicensesTable(); } ?? 为什么你要为DataContext分配一些空值? – sTrenat

+0

我想要挖掘一个空对象来处理datacontext。 –

回答

1

试试这个:

<dxg:GridControl x:Name="grdLicences" ItemsSource="{Binding Path=DataContext.LicensesTable, RelativeSource={RelativeSource AncestorType=Window}}" Grid.Row="1"/> 

它应该工作提供了窗口DataContextLicensesTable属性。

+0

嗨mm8,感谢您的帮助,它使用相对资源。即时通讯工作在做这样的事情: 我有网格(现在显示许可证),当你选择一个许可证,它显示了另一个控制中的许可证的细节。 我的主窗口有网格和2个小控件,用于选择许可证的详细信息。 我会继续努力,谢谢你的帮助 –