开发者

Binding "Text-Property" of a derived textbox to another textbox doesn´t work

i have a class 'MyTextBox' that derives from the default TextBox in Silverlight. This class currently contains no additional code.

I set up a binding in xaml to bind the Text-Property of MyTextbox to another Textbox to reflect the input made in the Textbox. The effect is that MyTextBox doesn´t update and not display the text of the other Textbox.

Additional i made an equal binding for a normal Textbox. And this works.

Here´s the XAML for the bindings.

<UserControl x:Class="Silverlight.Sample.Dummy"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.co开发者_高级运维m/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:my="clr-namespace:Sample"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">

<StackPanel x:Name="LayoutRoot" Background="White">
    <TextBox Height="23" x:Name="textBox2"  Width="120" />
    <TextBox Text="{Binding ElementName=textBox2, Path=Text, Mode=TwoWay}" Width="120" />
    <my:NumberTextBox Width="120" Text="{Binding ElementName=textBox2, Path=Text, Mode=OneWay}" />
</StackPanel>

Is there something special to set for binding, when i derive from a control.

PS: I tried a binding to a dummy object with INotifyPropertyChanged and set it as DataContext for the existing Textboxes. This test works as expected and my derived textbox gets updated.


Jehof,

First of all, use TwoWay binding on the Text property of your derived my:NumberTextBox.

If that doesn't solve the problem, continue on...

Does the my:NumberTextBox class actually derive from TextBox? If so, there shouldn't be anything else you need to do. If you are building your own control, and implmenting the Text property in your my:NumberTextBox class, try making it a DependencyProperty to allow TwoWay binding. As an example:

    // The Text dependency property
    #region The Text dependency property
    public static readonly DependencyProperty TextProperty =
        DependencyProperty.Register("Text", typeof(string), typeof(NumberTextBox),
        null);

    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }
    #endregion

Good luck, Jim McCurdy Face To Face Software and YinYangMoney


Finally, i found the reason, why it doesn´t work.

I made the mistake, that i added a new Silverlight User Control (MyTextBox) to my Project and changed the base class from UserControl to TextBox (with the corresponding Xaml-File). In this case the binding doesn´t work.

After i added a normal class to my project and derived this from TextBox, the binding works without any problems.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜