2017-09-01 197 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個小控件,用於選擇許可證的詳細信息。 我會繼續努力,謝謝你的幫助 –