Silverlight自定义用户控件和依赖属性问题

时间:2022-08-15 16:04:14

I have a custom user control named Field:

我有一个名为Field的自定义用户控件:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <TextBlock Grid.Row="0" Grid.Column="0" Name="LblName"/>
    <TextBox Grid.Row="0" Grid.Column="1" HorizontalContentAlignment="Left" Name="TxtValue"/>
</Grid>

In the CS file I expose the below property that simply sets the ".text" value of the textbox.

在CS文件中,我公开了下面的属性,它只是设置文本框的“.text”值。

    public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(
        "Value",
        typeof(string),
        typeof(Field),
        new PropertyMetadata(
            default(string),
            (source, args) => (source as Field).TxtValue.Text = (string)args.NewValue
        )
    );

    [Category("Custom")]
    public string Value
    {
        get
        {
            return (string)this.GetValue(ValueProperty);
        }
        set
        {
            this.SetValue(ValueProperty, value);
        }
    }

First question. Is this the correct way in which we propagate child user control dependancy properties, backup through the parent user control so that it can be set from the page xaml? I've had a look around on the net, there isn't much there considering that this is a fairly primitive thing todo when writing custom controls.

第一个问题。这是我们传播子用户控件依赖属性的正确方法,通过父用户控件进行备份,以便可以从页面xaml进行设置吗?我在网上看了一下,考虑到在编写自定义控件时这是一个相当原始的东西。

Giving details around the bigger picture, I have customer details page, some the controls are bound to the customer object which is contained in an observable collection. If I change the value of the text box (which is contained within the Field control) on the page from "xxxx" to "yyyy" I always get back "xxxx". It seems like something is going wrong somewhere around the getter?

提供详细信息,我有客户详细信息页面,一些控件绑定到可观察集合中包含的客户对象。如果我将页面上的文本框(包含在Field控件中)的值从“xxxx”更改为“yyyy”,我总是返回“xxxx”。看起来好像在吸气器附近出现了什么问题?

1 个解决方案

#1


This is definitely a step in the right direction for allowing properties to propagate.

这绝对是允许属性传播的正确方向上的一步。

If this is a true custom control (derives from Control), you'd want to use a {TemplateBinding Value} plus you should probably also connect to the TextChanged event on the TextBox, after setting up the template part in the OnApplyTemplate method. That way, you can have it also update your underlying TxtValue dependency property as well.

如果这是一个真正的自定义控件(从Control派生),你想要使用{TemplateBinding Value}加上你应该也可以在OnApplyTemplate方法中设置模板部分之后连接到TextBox上的TextChanged事件。这样,你也可以让它也更新你的底层TxtValue依赖属性。

If this is a UserControl, you may be able to use Binding, but you do have a few less options to go to. As you have it, your property changed callback function is only going in one direction: you only set the Text value, you never react to it.

如果这是一个UserControl,您可以使用Binding,但是您可以使用更少的选项。如你所知,你的属性改变了回调函数只朝一个方向:你只设置Text值,你永远不会对它做出反应。

Let's hope the documentation out there improves.

我们希望那里的文档有所改进。

#1


This is definitely a step in the right direction for allowing properties to propagate.

这绝对是允许属性传播的正确方向上的一步。

If this is a true custom control (derives from Control), you'd want to use a {TemplateBinding Value} plus you should probably also connect to the TextChanged event on the TextBox, after setting up the template part in the OnApplyTemplate method. That way, you can have it also update your underlying TxtValue dependency property as well.

如果这是一个真正的自定义控件(从Control派生),你想要使用{TemplateBinding Value}加上你应该也可以在OnApplyTemplate方法中设置模板部分之后连接到TextBox上的TextChanged事件。这样,你也可以让它也更新你的底层TxtValue依赖属性。

If this is a UserControl, you may be able to use Binding, but you do have a few less options to go to. As you have it, your property changed callback function is only going in one direction: you only set the Text value, you never react to it.

如果这是一个UserControl,您可以使用Binding,但是您可以使用更少的选项。如你所知,你的属性改变了回调函数只朝一个方向:你只设置Text值,你永远不会对它做出反应。

Let's hope the documentation out there improves.

我们希望那里的文档有所改进。