简体   繁体   中英

Use a binding to set the text property of a textbox in a usercontrol - WPF

I have a user control which contains a textbox and have created a get/set in the usercontrol to get/set the text property of the textbox.

public class OpenFileControl : UserControl
{
    StackPanel sp;
    public TextBox tb;

    public string Text { get { return tb.Text; } set { tb.Text = value; } }

I then want to set this value based on a binding later on -

<gX3UserControls:OpenFileControl Text="{Binding Value}"  />

But I get the following exception A 'Binding' cannot be set on the 'Text' property of type 'OpenFileControl'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.

After some investigation It seems Text needs to be a dependency property, but If I do that I cant work out how to pass the value on to the textbox.

How can I fix this.

Consider using something like this.

Control XAML:

<UserControl x:Class="WpfTestBench.OpenFileControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel>
        <TextBox Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, 
            Path=Filename, UpdateSourceTrigger=PropertyChanged}" />
    </StackPanel>
</UserControl>

Control codebehind:

using System.Windows;

namespace WpfTestBench
{
    public partial class OpenFileControl
    {
        public static readonly DependencyProperty FilenameProperty =
            DependencyProperty.Register("Filename", typeof (string), typeof (OpenFileControl));

        public OpenFileControl()
        {
            InitializeComponent();
        }

        public string Filename
        {
            get { return (string)GetValue(FilenameProperty); }
            set { SetValue(FilenameProperty, value); }
        }
    }
}

Main XAML:

<Window x:Class="WpfTestBench.OpenFileWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:wpfTestBench="clr-namespace:WpfTestBench"
        Title="OpenFileWindow" Width="300" SizeToContent="Height">
    <StackPanel>
       <wpfTestBench:OpenFileControl x:Name="In" Filename="{Binding SelectedFilename, UpdateSourceTrigger=PropertyChanged}" />
       <wpfTestBench:OpenFileControl x:Name="Out" Filename="{Binding ElementName=In, Path=Filename}" />
    </StackPanel>
</Window>

Main codebehind:

namespace WpfTestBench
{
    public partial class OpenFileWindow
    {
        public OpenFileWindow()
        {
            InitializeComponent();

            DataContext = this;
        }

        public string SelectedFilename { get; set; }
    }
}

Execution result (after typing something in the first control):

结果

If you define the dependency property as the static and the actual property, you can write whatever code behind you want in the body of the property.

public const string TextPropertyName = "Text";
public string Text
    {
        get
        {
            return (string)GetValue(TextProperty);
        }
        set
        {
            SetValue(TextProperty, value);
        }
    }

 public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
        TextPropertyName,
        typeof(string),
        typeof(MyControl),
        new UIPropertyMetadata(false));

In the getter and setter you can do something like textBox1.Text = value; but you'd probably be better served using a binding to the property instead. MVVM frameworks make light work of this sort of thing quite often. You might find more success defining a ViewModel (a class with an appropriate FielPath variable for example) and setting the DataContext of the new UserControl to be an instance of the ViewModel class, using Bindings to do the heavy lifting for you.

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