从父datacontex WPF绑定用户控件

时间:2017-09-01 08:02:15

标签: wpf controls model-binding

我试图从我的主窗口绑定/链接数据网格 的MainForm:

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

我想要的是在usercontrol中的网格中显示表。 这是可能的吗?

我在主窗口后面的代码中尝试了这个:

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未重新协调或无法访问。

运行代码的一部分,但是我觉得它使用控件的方式不正确。

此致

1 个答案:

答案 0 :(得分:1)

试试这个:

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

如果窗口的DataContext具有LicensesTable属性,它应该可以工作。