【问题标题】:WPF control binding to a single variableWPF 控件绑定到单个变量
【发布时间】:2018-11-21 11:23:36
【问题描述】:

我似乎无法将控件的值绑定到对象。我想将TextBox 绑定到string 对象,想法是当文本框的文本更改时,它也应该自动更改对象。无法弄清楚我做错了什么。这是我尝试过的:

MainWindow.xaml.cs:

public partial class MainWindow : Window
    {
        string str;
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = str;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {

    }
}

和 MainWindow.xaml:

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="150" Width="150">
    <Grid Margin="0,0,642,319">
        <TextBox HorizontalAlignment="Left" Height="23"  TextWrapping="Wrap" Text="{Binding str}" VerticalAlignment="Top" Width="120" Margin="0,0,-120,-46" />
        <Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Click="Button_Click" Height="23" Margin="0,28,-75,-51" RenderTransformOrigin="0.423,2.257" />

    </Grid>
</Window>

所以,当我在文本框中输入内容并单击按钮时,我应该在调试时看到 str 中的文本,但它始终是 null

【问题讨论】:

  • 如果您为其指定名称,则可以直接在代码中访问文本框:&lt;TextBox x:Name="MyTextBox" HorizontalAlignment="Left" Height="23" ... /&gt;。在后面的代码中:var x = MyTextBox.TextMyTextBox.Text = "Hello World"
  • @croxy,谢谢,我知道。但问题是,我有一个由 5-6 个控件组成的完整表单。我不想在最后手动单独处理所有这些。
  • 这个答案可能对你有帮助:stackoverflow.com/a/6022711/5453249
  • 从这里开始阅读:Data Binding Overview。您应该创建一个具有公共字符串属性的类并将其实例分配给 DataContext。
  • 声明一个公共的自动属性 ​​str - public string { get;放;并设置 DataContext = this;

标签: c# wpf data-binding


【解决方案1】:
  1. 将 str 更改为自动属性:

    公共字符串 str { 获取;放; }

  2. 将 DataContext 更改为:

    DataContext = this;

DataContext 是保存绑定属性/命令/事件的类。 属性/命令/事件需要公开才能被您的视图访问。

要使双向绑定起作用,您必须通知 UI 绑定该属性已更改,为此您需要为包含已在 UI 中绑定的属性的类实现 INotifyPropertyChanged 接口.您将需要私有财产,并且无法从自动财产通知。

简单示例:

public class Sample : INotifyPropertyChanged
{
    private string _str;
    public string Str
    {
        get { return _str; }
        set
        {
            _str = value;
            NotifyPropertyChanged(nameof(Str));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged(string propName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
    }
}

【讨论】:

  • 自动属性仅在您不需要收到有关属性值更改的通知时才有效。然后,您将使用带有支持字段的属性,并在属性设置器中触发 INotifyPropertyChanged 接口的 PropertyChanged 事件。
  • 这行得通,但最好 DataContext 应该是 ViewModel。你可能会在这里得到一些灵感:stackoverflow.com/questions/46931212/…
  • 我怎样才能让它双向工作?我的意思是,当我更改 string 对象时,它应该更改 Textbox's 文本我尝试设置 Mode=TwoWay
  • 您必须为 MainWindow 类实现 INotifyPropertyChanged 接口,并使用属性名称“str”调用事件处理程序。搜索 notify property changed wpf 即可得到代码。
【解决方案2】:

首先,WPF 中的数据绑定仅适用于公共属性。所以你必须在后面的代码中明确声明一个(而不是string str;

public string str { get; set; }

其次,视图的DataContext 属性定义了将在其中搜索属性以查找绑定的对象/类。您示例中的行this.DataContext = str; 意味着您希望在str 对象(即string)中查找视图中的绑定。您应该将这一行替换为

this.DataContext = this;

以便在该视图本身的代码中搜索绑定。

备注 如果str 是公共属性,您也可以保留this.DataContext = str; 行并使用诸如

的表达式进行绑定
<TextBox Text="{Binding .}" />

这将绑定到 DataContext 属性的值。

【讨论】:

    【解决方案3】:

    也许您可以使用 MVVM light 进行绑定。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-06
      • 1970-01-01
      • 1970-01-01
      • 2014-08-25
      • 2011-05-31
      • 2014-10-31
      • 2011-07-26
      • 2011-01-06
      相关资源
      最近更新 更多