如何动态放置和调整控件大小 [英] How to place and resize controls dynamically

查看:78
本文介绍了如何动态放置和调整控件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在wpf窗口上有许多控件,用户可以移动/调整大小。 我将已调整大小的信息保存在项目数据库中,并希望在重新打开窗口时使用该信息来放置控件。 除了最后一步之外,我在
处有所有这个过程,即在重新打开窗口时放置控件。



例如,假设我从数据库返回以下字符串:

控制名称:MyTextBox

属性:宽度

价值:200



在伪代码中,我想要的是这样的:

I have a number of controls on a wpf window that the user can move/resize.  I save the resized information in the project database and want to use that information to place the controls when the window is reopened.  I have all that process in place except for the very last step, namely, placing the controls when the window is reopened.

For example, suppose I return from the database the following strings:
Control Name: MyTextBox
Property: Width
Value: 200

In pseudo-code what I want is something like this:

Me.Controls(MyTextBox).Property(Width).Value = 200


我已经搜索了这个例子但是没有成功。 如何做到这一点?

I have search for examples of this but have been unsuccessful.  How can this be done?

推荐答案

嗨  SezMe,



>>例如,假设我从数据库返回以下字符串:

控件名称:MyTextBox

属性:宽度

价值:200



在伪代码中我想要的是这样的:



您可以使用以下步骤来实施它。



1:查找控件



2:目标特定控制权b


3:获取修改后的财产并转让给它


您可以参考以下示例并尝试实现您自己的详细信息。

Hi  SezMe,

>>For example, suppose I return from the database the following strings:
Control Name: MyTextBox
Property: Width
Value: 200

In pseudo-code what I want is something like this:

You can use the following steps to implement it.

1: Find the Controls

2: Target specific controls

3: Get the modified property and assign it

You can refer the following sample and try to Implement your own details.

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            foreach (TextBox tb in FindVisualChildren<TextBox>(this))
            {
                if (tb.Name == "MyTextBox")
                {
                    Type controlType = tb.GetType();
                    PropertyInfo propertyInfo = typeof(TextBox).GetProperty("Width");
                    propertyInfo.SetValue(tb, 656);
                }
            }

        }


        public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
        {
            if (depObj != null)
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                    if (child != null && child is T)
                    {
                        yield return (T)child;
                    }

                    foreach (T childOfChild in FindVisualChildren<T>(child))
                    {
                        yield return childOfChild;
                    }
                }
            }
        }


PropertyInfo.SetValue Method
$


$
此外,如果您可以通过标记有用的帖子来关闭该帖子,将不胜感激一个答案。如果他们遇到类似问题,这将有助于其他成员快速找到解决方案。如果你有新的问题,你可以开始一个新的线程。



$


最好的问候,



Yong Lu

PropertyInfo.SetValue Method


Besides, It would be appreciated if you could close the thread by marking helpful posts as an answer. This will help other members to find the solution quickly if they have faced the similar issue. If you have a new question you can start a new thread.



Best Regards,

Yong Lu


这篇关于如何动态放置和调整控件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆