如何在Windows Phone中为我的自定义控件添加双向绑定?

时间:2013-07-26 09:36:02

标签: binding user-controls custom-controls windows-phone two-way

我想在我的customcontrol中使用twoway绑定。这是代码;

CustomControl;

<Grid>
        <PasswordBox x:Name="passwordB" GotFocus="PasswordBox_GotFocus" LostFocus="PasswordBox_LostFocus" PasswordChanged="passwordB_PasswordChanged" Style="{StaticResource AkbankControlStyleWatermarkPasswordBoxLoginFormInputPasswordBox}"></PasswordBox>
        <TextBlock x:Name="lblWaterMark" MouseLeftButtonDown="lblWaterMark_Tapped" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="20,10,20,10" Opacity="0.8" FontFamily="Segoe UI" FontSize="16" Foreground="#FF8E8E8E" FontWeight="SemiBold"></TextBlock>
</Grid>

它的名字是WatermarkPasswordTextBox :) 的DependencyProperty;

public static readonly DependencyProperty PasswordProperty =
        DependencyProperty.Register(
            "PassText",
            typeof(string),
            typeof(WatermarkPasswordTextBox),
            new PropertyMetadata(""));

属性;

    private string _passText = "";
    public string PassText
    {
        get
        {
            if (passwordB != null)
            {
                _passText = passwordB.Password;
                return _passText;
            }
            else
            {
                return String.Empty;
            }

        }
        set
        {
            if (passwordB != null)
            {
                SetProperty<string>(ref _passText, value, "PassText");
                passwordB.Password = _passText;
                passwordB_PasswordChanged(passwordB, null);
            }
            else
            {
                SetProperty<string>(ref _passText, value, "PassText");
            }
        }
    }

OnApplyTemplate;

   public override void OnApplyTemplate()
    {

        base.OnApplyTemplate();
        this.SetBinding(
            WatermarkPasswordTextBox.PasswordProperty,
            new Binding
            {
                Path = new PropertyPath("PassText"),
                Mode = BindingMode.TwoWay,
                Source = this
            });
    }

我的Xaml;

  <CustomControls:WatermarkPasswordTextBox
        PassText="{Binding Password,Mode=TwoWay}"
        Padding="5" 
        x:Name="CustomerPasswordTextBox"
        x:FieldModifier="public"
        LenghtMax="6"
        Watermark="{Binding LocalizedResources.PasswordWatermarkWatermark,Source={StaticResource LocalizedStrings}}" 
        RelayedKeyUp="CustomerPasswordTextBox_KeyUp"
        HorizontalContentAlignment="Left"/>

错误代码;

  System.ArgumentException: Object of type 'System.Windows.Data.Binding' cannot be converted to type 'System.String'.

此代码提供运行时错误。

感谢。

0 个答案:

没有答案