1

I want to use twoway binding in my customcontrol. It's codes;

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>

It's name is WatermarkPasswordTextBox :) DependencyProperty;

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

Properties;

    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
            });
    }

My 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"/>

Error Code;

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

This code is giving runtime error.

Thanks.

Tugrul Emre Atalay
  • 918
  • 1
  • 9
  • 28

0 Answers0