【问题标题】:Binding a usercontrol to SQL datatable将用户控件绑定到 SQL 数据表
【发布时间】:2014-01-05 02:35:47
【问题描述】:

我有一个用户控件,它应该查看 SQL 数据表中的一些数据。我的数据库类名为 DBManager,它有一个名为 ResTable 的属性,其类型为 Datatable。它具有与我的用户控件相关的数据。在检索表时,我想将一些控件实例化为数据表中的行数,并将每个字段与表中的相应字段绑定。我知道如何绑定组合框,但不知道用户控件。

<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 的格式相同,但我也不知道如何填充它。

【问题讨论】:

    标签: c# sql wpf datatable


    【解决方案1】:

    你可以让用户控件的构造函数接受参数。然后,您可以将用户控件的控件绑定到传递的参数。示例:

     public partial class TestControl : UserControl
     {
        public TestControl()
        {
            InitializeComponent();
        }
    
    
        public TestControl(string name)
        {
            lblName.Content = name;
            InitializeComponent();
        }
     }
    

    将其用作:

    TestControl tc = new TestControl("Austin");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-21
      • 2013-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-28
      • 2011-08-11
      相关资源
      最近更新 更多