2013-12-17 96 views
0

我有一個用戶控件應該查看SQL數據表中的一些數據。我的數據庫類叫做DBManager,它有一個叫做ResTable的屬性,它的類型是Datatable。它具有與我的用戶控件中的內容相關的數據。在檢索表格後,我想將一些控件實例化爲Datatable中的行數,並將每個字段與表中相應的字段進行綁定。我知道如何綁定組合框但不是usercontrols。將usercontrol綁定到SQL數據表

<UserControl 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
xmlns:local="clr-namespace:Menupedia" 
mc:Ignorable="d" 
x:Class="Menupedia.RestaurantViewer" 
x:Name="UserControl" 
d:DesignWidth="640" d:DesignHeight="480" Width="330" Height="150"> 
<Grid x:Name="LayoutRoot" Width="330"> 
    <Label x:Name="label_Name" Content="" HorizontalAlignment="Left" Height="30" Margin="16,0,0,7" VerticalAlignment="Bottom" Width="72" FontFamily="Public Enemy NF" FontSize="14.667" Foreground="#FFEF7B54"/> 
    <Image x:Name="image_Logo" HorizontalAlignment="Left" Margin="16,13,0,37" Width="100"/> 
    <Label x:Name="label_Phone" Margin="0,61,122,59" FontFamily="Public Enemy NF" FontSize="14.667" Foreground="#FFEF7B54" HorizontalAlignment="Right" Width="42" Content="{Binding Hotline}"/> 
    <ComboBox x:Name="combobox_Branch" Height="30" Margin="0,21,29,0" Style="{DynamicResource MPComboBox}" VerticalAlignment="Top" HorizontalAlignment="Right" Width="135"/> 
    <Image Margin="126,55,0,59" Source="Images/Phone.png" Stretch="Fill" HorizontalAlignment="Left" Width="36"/> 
    <Image Margin="120,13,164,0" Source="Images/Branch.png" Stretch="Fill" Height="46" VerticalAlignment="Top"/> 
    <Label x:Name="label_labelCuisines" Content="Cuisine:" Height="30" Margin="125,0,150,25" VerticalAlignment="Bottom" FontFamily="Public Enemy NF" FontSize="14.667" Foreground="#FFEF7B54"/> 
    <Label x:Name="label_Cuisines" Height="48" Margin="0,0,29,7" VerticalAlignment="Bottom" FontFamily="Public Enemy NF" FontSize="14.667" Foreground="#FFEF7B54" HorizontalAlignment="Right" Width="117"/> 
</Grid> 

這個用戶控件的相應的對象沒有屬性。我雖然有一個名爲Restaurant的類,它與我使用的Datatable的格式相同,但我不知道如何填充它。

回答

0

您可以使用戶控件的構造函數獲取參數。然後,您可以將用戶控件的控件綁定到傳遞的參數。例如:

public partial class TestControl : UserControl 
{ 
    public TestControl() 
    { 
     InitializeComponent(); 
    } 


    public TestControl(string name) 
    { 
     lblName.Content = name; 
     InitializeComponent(); 
    } 
} 

使用它作爲:

TestControl tc = new TestControl("Austin"); 
相關問題