【问题标题】:Binding cannot be converted to type System.String绑定无法转换为 System.String 类型
【发布时间】:2022-07-13 18:10:13
【问题描述】:

我正在尝试将用户控件中的属性绑定到窗口但出现错误

\'System.Windows.Data.Binding\' 类型的对象无法转换为 \'System.String\' 类型。

当我搜索错误时,通常人们没有创建依赖属性,或者问题可能是因为两种方式绑定但没有找到解决方案

我知道如果我给用户控件一个名称然后获取值但我想使用绑定

用户控件.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>


用户控件.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;
        }

主窗口.xaml

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

    标签: c# wpf data-binding user-controls


    【解决方案1】:

    依赖属性名称必须以“Property”结尾,而不是“Prop”:

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

    为所有用途和所有属性修复它。

    OnValueChangedText 回调实现不正确,应从元数据中删除

    【讨论】:

      猜你喜欢
      • 2012-09-07
      • 2014-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-14
      • 2017-11-24
      • 2019-12-07
      • 1970-01-01
      相关资源
      最近更新 更多