简体   繁体   中英

Binding cannot be converted to type System.String

I'm trying to bind a property from a user control to a window but get error

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

When I was searching the error usually the people didn't create a Dependency Property or that the problem may be because the two Way binding but didn't find a solution

I know that if I give the usercontrol a name and then get the value but I want to use binding

UserControl.xaml

<UserControl x:Name="ctb" x:Class="ChatClient.CustomControls.CustomTextBox"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:ChatClient.CustomControls"
             mc:Ignorable="d" 
             d:DesignHeight="20" d:DesignWidth="350">
    <Grid>
        <Border CornerRadius="8"
                            Background="#3e4147">

            <Grid>
                <TextBox VerticalAlignment="Stretch"
                                     VerticalContentAlignment="Center"
                                     HorizontalAlignment="Stretch"
                                     Background="Transparent"
                                     x:Name="TextBox"
                                     TextWrapping="Wrap"
                                     BorderThickness="0"
                                     Foreground="Gray"
                                     CaretBrush="Gray"
                                     Margin="8,0,0,0"
                                     Text="{Binding ElementName=ctb, Path=InputText}">

                </TextBox>

                <TextBlock IsHitTestVisible="False"
                                       Text="{Binding ElementName=ctb, Path=PlaceHolder}"
                                       VerticalAlignment="Center"
                                       HorizontalAlignment="Left"
                                       Margin="10,0,0,0"
                                       Foreground="DarkGray">
                    <TextBlock.Style>
                        <Style TargetType="TextBlock">
                            <Setter Property="Visibility" Value="Collapsed"/>
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Text, ElementName=TextBox}" Value="">
                                    <Setter Property="Visibility" Value="Visible"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </TextBlock.Style>
                </TextBlock>
            </Grid>
        </Border>
    </Grid>
</UserControl>


UserControl.xaml.cs

 public CustomTextBox()
        {
            InitializeComponent();
        }

        public static readonly DependencyProperty InputTextProp = DependencyProperty.Register(
                nameof(InputText),
                typeof(string),
                typeof(CustomTextBox),
                new PropertyMetadata(OnValueChangedText));

        public static readonly DependencyProperty PlaceHolderProp = DependencyProperty.Register("PlaceHolder", typeof(string),
                typeof(CustomTextBox),
                new PropertyMetadata(null));

        public string InputText
        {
            get { return (string)GetValue(InputTextProp); }
            set { SetValue(InputTextProp, value); }
        }
        public string PlaceHolder
        {
            get { return (string)GetValue(PlaceHolderProp); }
            set { SetValue(PlaceHolderProp, value); }
        }
        private static void OnValueChangedText(DependencyObject sender,DependencyPropertyChangedEventArgs e)
        {
            CustomTextBox customTextBox = sender as CustomTextBox;
            customTextBox.InputText = (string)e.NewValue;
        }

MainWindow.xaml

 <customcontrols:CustomTextBox InputText="{Binding UserName}"
                                          PlaceHolder="UserName"
                                          Margin="0,0,0,20" />

Dependency property name must end with "Property", not "Prop":

public static readonly DependencyProperty InputTextProperty = DependencyProperty.Register
(
    nameof(InputText),
    typeof(string),
    typeof(CustomTextBox),
    new PropertyMetadata(string.Empty)
);

fix it for all usages and all properties.

OnValueChangedText callback implemented incorrectly and should be removed from metadata

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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