【问题标题】:Creating Components And Setting It's Location创建组件并设置它的位置
【发布时间】:2017-06-02 18:45:20
【问题描述】:

我正在开发用于个人开支控制的财务管理 WPF 应用程序。我的应用程序中有一个名为“记录”的选项卡,它应该显示以前购买的产品的所有记录。我有这个 DAO 方法可以把所有的记录还给我。

 public List<TOProduct> LoadRecords(int id)
    {
        List<TOProduct> i = new List<TOProduct>();

    try
        {


            string sql = "select * from tbl_records where user_id = " + id + " and product_status = true";

            con = ConnectionFactory.Connection();

            MySqlCommand cmd = new MySqlCommand(sql, con);

            con.Open();

            MySqlDataReader dtreader = cmd.ExecuteReader();


                while (dtreader.Read())//If there's any data.
                {
                    TOProduct x = new TOProduct();

                    x.Id = dtreader.GetInt16("product_id");
                    x.Link = dtreader.GetString("product_link");
                    x.Name = dtreader.GetString("product_name");
                    x.Type = dtreader.GetString("product_type");
                    x.Price = dtreader.GetDouble("product_price");
                    x.Store = dtreader.GetString("product_store");
                    x.BuyingDate = dtreader.GetDateTime("product_buyingDate").ToString("dd-MM-yyyy"); ;

                    i.Add(x);

                }

                con.Close();

            }
        }
        catch (MySqlException e)
        {
            throw new Exception(e.Message);
        }

        return i;
    }

效果很好。现在,在我看来,我想创建一个包含三个标签的面板:产品名称标签、产品价格标签和产品购买日期标签。问题是,我想为从我的方法返回的每个注册创建一个带有这三个标签的面板。另外,我需要一个滚动条,因为用户可以有 1 或 1000 条记录,以防屏幕不适合。这些面板必须排列成一个列表。我知道我可以创建某种for(int i = 0; int i &lt; number_of_records; i++)。问题是组件的创建及其在屏幕上的位置。

谁能告诉我是否有办法创建这些组件并根据我的需要在屏幕上设置它们的位置?如何?如果没有答案,我可能会创建一个DataGrid 并用我的LoadRecordsreturn 对象填充它,但是拥有这些组件真的很棒。提前致谢。

编辑:这是我想做的图片:

这是我用于显示您最近购买的 3 次“快速”菜单的设计。但它们是静态的,我只是用信息“喂”它们。如前所述,我想做一个更大的规模,动态创建每个组件并在屏幕上设置它的位置。

【问题讨论】:

  • 但您的问题的解决方案是使用 DataGrid/ GridView 。为什么要创建新组件。您也可以使用 listControl 并拥有 3 个属性,您可以更改列表框的模板
  • 以前从未使用过 listControl。可以按照我描述的方式使用吗?出于静态原因,我没有更多地使用 DataGrid。如果其他一切都失败了,我会使用。
  • 你使用的是winform还是web app?
  • 那为什么不使用 Grid 呢。根据您的要求,我的数据网格没有达到什么目的?
  • 这是一个设计问题。我会在一分钟内上传图片。

标签: c# wpf loops components


【解决方案1】:

我认为您可以使用 ListView 控件来激活它。您需要设置样式和 UI 格式,但这将是最好的。它还提供滚动功能。

  <Window x:Class="WpfTutorialSamples.ListView_control.ListViewItemTemplateSample"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="ListViewItemTemplateSample" Height="150" Width="350">
<Grid>
            <ListView Margin="10" Name="lvProducts">
                    <ListView.ItemTemplate>
                            <DataTemplate>
                                    <WrapPanel>

                                            <TextBlock Text="{Binding Name}" FontWeight="Bold" />
                                            <TextBlock Text=", " />

                                            <TextBlock Text="{Binding Price}" FontWeight="Bold" />
                                            <TextBlock Text=" (" />
                                            <TextBlock Text="{Binding Date}" TextDecorations="Underline" Foreground="Blue"  />
                                            <TextBlock Text=")" />
                                    </WrapPanel>
                            </DataTemplate>
                    </ListView.ItemTemplate>
            </ListView>
    </Grid>

【讨论】:

  • 您也可以使用列表框。因为你有单列。 ListView 用于不同的场景。
【解决方案2】:

您可以创建自定义用户控件。 [对于 Visual Studio 2015] 转到

  • Visual C# => Windows => 经典桌面并选择 WPF 用户控件 图书馆。
  • 在自定义控件中添加您希望拥有的控件。我添加了一个文本框和一个按钮。

创建控件后可访问的属性。要访问任何组件的事件,请声明事件并将事件处理程序添加到构造函数中的事件。

公共部分类 UserControl1:UserControl { 公共字符串 SearchTextBoxText { 得到{返回搜索文本框.文本; } 设置 { SearchTextBox.Text = 值; } }

    public object SearchButtonContent
    {
        get { return SearchButton.Content; }
        set { SearchButton.Content = value; }
    }

    public event RoutedEventHandler SearchButton_Clicked;

    public UserControl1()
    {
        InitializeComponent();
     SearchButton_Clicked += SearchButton_Click;
    }

    private void SearchButton_Click(object sender, RoutedEventArgs e)
    {
        if (SearchButton_Clicked != null)
        {
            SearchButton_Clicked(sender, e);
        }
    }
}  

现在构建解决方案。
PS:UserControl 项目必须与您的应用程序项目在同一个解决方案中。
现在在您的应用程序中添加对这个项目的引用,您就可以使用这个自定义控件了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-21
    • 2015-06-09
    • 1970-01-01
    • 2017-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多