简体   繁体   English

如何在运行时将值从窗口传递给用户控件?

[英]How to passing value from window to usercontrol at runtime?

This is my user control code behind where I should get the data: 这是我应该在哪里获取数据的用户控制代码:

这是我应该在哪里获取数据的用户控制代码

This is my main window where I included the user control and set the value to the property: 这是我包含用户控件并将其值设置为属性的主窗口:

这是我在其中包含用户控件并将值设置为属性的主窗口。

This doesn't work as the value is always null. 由于该值始终为null,因此无法使用。 Please help to correct anything that I've done wrong. 请帮助纠正我做错的任何事情。 Thanks. 谢谢。

It looks like you need to use a dependency property as far as I know. 据我所知,您似乎需要使用依赖项属性。 This will replace the GetMyValue property you have. 这将替换您拥有的GetMyValue属性。

check out this example for custom dependency properties. 查看此示例以获取自定义依赖项属性。

https://www.tutorialspoint.com/wpf/wpf_dependency_properties.htm https://www.tutorialspoint.com/wpf/wpf_dependency_properties.htm

Side Note: a quick way to make one is to type "propdp" and then tab twice. 旁注:一种简单的输入方法是键入“ propdp”,然后两次制表符。 Then tab your way through setting it up. 然后通过设置方式进行选择。

1.Make sure you had set the Scope on 'User' rather than 'Admin', otherwise you won't have writing access to Recourses. 1.确保您将范围设置为“用户”而非“管理员”,否则您将无权访问资源。 2.Make sure you have saving method after modifying data. 2.确保在修改数据后有保存方法。

For writing using Properties.Settings; 用于使用Properties.Settings编写;

Settings.Default.myProperty = myValue;
Settings.Default.Save();

For Reading 阅读

String myValue = Settings.Default["myProperty"].ToString();

You can also manage your properties from Solution Explore > Your Project > Properties > Settings.settings 您还可以从解决方案浏览器>项目>属性>设置来管理属性。

One good solution for doing this: 一个好的解决方案:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    var control = new UserControl1();
    control.GetMyValue = "HelloWorld";
    grid1.Childern.Add(control);
}

There are other solutions, like Binding DataContext or making a custom DependencyProperty 还有其他解决方案,例如绑定DataContext或创建自定义DependencyProperty

The UserControl is created before the GetMyValue property is set. 在设置GetMyValue属性之前创建UserControl You cannot set a property of an instance before you have created it... 创建实例之前,无法设置实例的属性...

Wait until the UserControl has been loaded and you will get the value as expected: 等待直到UserControl加载完毕,您将获得预期的值:

public UserControl1()
{
    InitializeComponent();
    Loaded += (s, e) =>
    {
        string finalValue = GetMyValue;
    };
}

You need to create Dependency property. 您需要创建Dependency属性。 There's no hard to do: 没有什么难做的:

First you need to register: 首先,您需要注册:

public static readonly DependencyProperty GetMyValueProperty =
            DependencyProperty.Register("GetMyValue", typeof(string), 
            typeof(UserControl1), new UIPropertyMetadata(string.Empty));  

Then create auto-property: 然后创建自动属性:

public string GetMyValue
        {
            get { return (string )GetValue(GetMyValueProperty ); }
            set { SetValue(GetMyValueProperty , value); }
        }

That's all, just copy this examples into your UserControl1 class. 就是这样,只需将示例复制到UserControl1类中即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM