从父数据上下文 WPF 绑定用户控件

Binding user control from parent datacontex WPF

我正在尝试 bind/link 来自主 window 的数据网格 主窗体:

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

我将控件分配到主网格的一行中 window。 主要 window 链接(数据上下文)到一个具有此方法

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

我想要的是在用户控件的网格中显示 table。 有可能吗?

我在主 window 背后的代码中尝试了这个:

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 未重新识别或不可访问。

运行它背后的代码部分,但我认为我使用控件的方式不正确。

此致

试试这个:

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

如果 window 的 DataContext 有一个 LicensesTable 属性。