简体   繁体   English

如何将xaml绑定到其他类的文本框

[英]How to bind in xaml to a textbox from a different class

I can figure out how to bind a property to a textbox in the codebehind, but with my current application I need to bind to a property from a different class. 我可以在后面的代码中找出如何将属性绑定到文本框,但是对于我当前的应用程序,我需要从其他类绑定到属性。 Here's a simplified version of what I have: 这是我所拥有的简化版本:

<Window x:Class="Project1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
<Grid>
    <TextBox x:Name="Textbox1" Text="{Binding Class1.Class2.TextToBind, Mode=TwoWay}" Height="20"  Width="75" Background="#FFE5E5E5"/>
</Grid>

Codebehind: 代码背后:

namespace Project1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            Class1 = new Class1();
        }
        public Class1 Class1 { get; set; }
    }
}

Class1: 第1类:

namespace Project1
{
    public class Class1
    {
        public Class1()
        {
            Class2 = new Class2();
        }
        public Class2 Class2 { get; set; }
    }
}

Final class: 期末课程:

namespace Project1
{
    public class Class2
    {
        public Class2()
        {
        }
        private string textToBind;
        public string TextToBind { get { return textToBind; } set { SetProperty(ref textToBind, value); } }

        public event PropertyChangedEventHandler PropertyChanged;
        private void SetProperty<T>(ref T field, T value, [CallerMemberName] string name = "")
        {
            if (!EqualityComparer<T>.Default.Equals(field, value))
            {
                field = value;
                var handler = PropertyChanged;
                if (handler != null)
                {
                    handler(this, new PropertyChangedEventArgs(name));
                }
            }
        }
    }
}

You have to set DataContext for your TextBox or for your Window 您必须为您的TextBox或Window设置DataContext

namespace Project1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            Class1 = new Class1();


            Class1.Class2.TextToBind = "Test";

            this.DataContext = this;
        }
        public Class1 Class1 { get; set; }
    }
}

Also you need to inherit Class2 from INotifyPropertyChanged: 另外,您还需要从INotifyPropertyChanged继承Class2:

public class Class2 : INotifyPropertyChanged
{
    public Class2()
    {
    }
    private string textToBind;
    public string TextToBind { get { return textToBind; } set { SetProperty(ref textToBind, value); } }

    public event PropertyChangedEventHandler PropertyChanged;
    private void SetProperty<T>(ref T field, T value, [CallerMemberName] string name = "")
    {
        if (!EqualityComparer<T>.Default.Equals(field, value))
        {
            field = value;
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }
    }
}

If you want to bind your textbox to a property from a class other than the datacontext of the window, you need to set it explicitly 如果要将textbox绑定到窗口数据上下文以外的类的属性,则需要显式设置它


I tried this code and it worked 我尝试了这段代码,它起作用了

<Window x:Class="Project1.MainWindow"
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

      <!-- first add namespace of your project-->    
    xmlns:local="clr-namespace:Project1"

    Title="Window1" Height="300" Width="300">

   <!--second define your data context class as resource-->
    <Window.Resources >
        <local:Class2 x:Key="class2"></local:Class2>
    </Window.Resources>

    <Grid>
        <TextBox x:Name="Textbox1" Text="{Binding TextToBind, Mode=TwoWay}" Height="20"  Width="75" Background="#FFE5E5E5">


           <!--third set the data context of the textbox Explicitly-->
            <TextBox.DataContext>
                <StaticResourceExtension ResourceKey="class2"/>
            </TextBox.DataContext>
        </TextBox>
    </Grid>
</Window>

Please note : if you are going to set the property TextToBind programmatically and you want your UI to show the result, you have to implement INotifyPropertyChanged . 请注意 :如果要以编程方式设置属性TextToBind并且希望UI显示结果,则必须实现INotifyPropertyChanged

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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