2017-09-01 3 views
0

Im essayant de lier/lier un DataGrid de ma fenêtre principale MainForm:contrôle utilisateur liaison de datacontex parent WPF

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

J'ai créé ce contrôle:

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

J'ai demandé le contrôle dans une rangée de la grille dans la fenêtre principale. La fenêtre principale est liée (contexte de données) à une classe qui a ces méthodes

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(); } 
    } 

ce que je veux est d'afficher la table dans la grille qui est dans le usercontrol. C'est possible?

Je tryed cela dans le code derrière ma fenêtre principale:

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(); 
     } 
    } 

il est dit que tblLicenses ne reconized ou non accesible. Exécuter la partie du code behin cela fonctionne mais je pense que c'est incorrect la façon dont im en utilisant le contrôle.

Cordialement

+0

C'était assez difficile de comprendre ce que vous voulez faire. Vous avez besoin d'une propriété attachée ou dépendante dans votre UC, (pour vous le 2ème sera meilleur). Lorsque vous le créez, vous pouvez l'assigner à "tblLicences", puis lier ce que vous voulez. Et la deuxième chose: pourquoi avez-vous écrit: sinon { this.DataContext = _CustomerLicense.LicensesTable(); } ?? Pourquoi assignez-vous quelque chose à DataContext si c'est null? – sTrenat

+0

Je voulais associer un objet vide au datacontext pour travailler avec. –

Répondre

1

Essayez ceci:

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

Il devrait fonctionner à condition que le DataContext de la fenêtre a une propriété LicensesTable.

+0

Salut mm8, merci de l'aide, ça fonctionne en utilisant la relativeresource. Im travaillant en faisant quelque chose comme ceci: J'ai la grille (montre maintenant les licences) lorsque vous sélectionnez une licence, il montre le détail de la licence dans un autre contrôle. Mes fenêtres principales ont la grille et 2 petites commandes pour les détails de la licence sélectionnée. Je continuerai à travailler dessus, merci pour votre aide –