【问题标题】:Binding TextBox Text to property in code behind file将文本框文本绑定到文件后面代码中的属性
【发布时间】:2015-12-30 17:15:08
【问题描述】:

我有一个 UserControl 文件,该文件有一个文本框,我想将它绑定到代码隐藏文件中的一个属性,但由于某种原因,我无法将其绑定。有人可以告诉我我做错了什么。提前致谢。

XAML:

<ContentDialog  Width="200" Height="400" Background="White" Padding="-40,-20" x:Name="addGreetingDialog" PrimaryButtonText="Save" SecondaryButtonText="Cancel" PrimaryButtonClick="addGreetingDialog_PrimaryButtonClick">


    <Grid Margin="-25,0,-25,0">
        <Grid.RowDefinitions>
            <RowDefinition Height="50"></RowDefinition>
            <RowDefinition Height="40"></RowDefinition>
            <RowDefinition Height="40"></RowDefinition>
        </Grid.RowDefinitions>

        <Border Grid.Row="0">
            <Border.Background>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="#FF686868" Offset="0"/>
                    <GradientStop Color="#FF515151" Offset="1"/>
                    <GradientStop Color="#FF676767" Offset="0.5"/>
                </LinearGradientBrush>
            </Border.Background>
            <TextBlock HorizontalAlignment="Center" FontSize="24" Foreground="White"  FontFamily="ms-appx:/Assets/Fonts/Roboto-Light.ttf#Roboto"
                       Margin="10">Add Greetings</TextBlock>
        </Border>
        <TextBox Grid.Row="2" PlaceholderText="Greeting" Text="{Binding NewGreeting, Mode=TwoWay}"></TextBox>
    </Grid>
</ContentDialog>

代码背后:

 private string _newGreeting;
 public string NewGreeting { get { return _newGreeting; } set { _newGreeting = value; } }

 public AddGreeting()
 {

     this.InitializeComponent();
 }

 private async void addGreetingDialog_PrimaryButtonClick(global::Windows.UI.Xaml.Controls.ContentDialog sender, global::Windows.UI.Xaml.Controls.ContentDialogButtonClickEventArgs args)
 {
 }

【问题讨论】:

  • 你在设置数据上下文吗?
  • 没有。我不知道我必须这样做。你能告诉我你的意思吗? @GabrielDuarte

标签: c# xaml binding


【解决方案1】:

我想通了。我错过了这条线。

DataContext="{Binding RelativeSource={RelativeSource Self}}

【讨论】:

    【解决方案2】:

    更新一下

         public AddGreeting()
     {
    
         this.InitializeComponent();
     }
    

     public AddGreeting()
     {
    
         this.InitializeComponent();
         DataContext = this;
     }
    

    这:DataContext = this; 将告诉表单在 AddGreeting 类中查找指定的属性,我想这是 NewGreeting 属性所在的位置。如果没有,例如,让我们有以下类:

        public class YourClass
        {
            private string _newGreeting;
            public string NewGreeting { get { return _newGreeting; } set { _newGreeting = value; } }
    
            public YourClass()
            {
                NewGreeting = "Test";
            }
        }
    

    这样,您必须像这样在 CodeBehind 表单中设置 DataContext:DataContext = new YourClass(); 而不是 DataContext = this;。这样你就告诉表单在 YourClass 类而不是 AddGreeting 类中查找属性。

    更多信息,我建议你看this tutorial

    【讨论】:

    • 请注意,这会阻止从父控件继承 DataContext。或者设置 Binding 的 ElementName 属性。
    猜你喜欢
    • 2019-08-07
    • 2017-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-12
    • 1970-01-01
    • 2011-01-13
    • 1970-01-01
    • 2013-03-21
    相关资源
    最近更新 更多