kur*_*a88 2 c# wpf xaml binding textbox
我很抱歉这个问题很基本。我刚刚学习了WPF,但我无法通过简单的两种方式将textbox.text绑定为字符串属性。
XAML代码:
<Window x:Class="WpfApplication1.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:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid x:Name="StuInfo">
<TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="10,26,0,0" TextWrapping="Wrap" Text="{Binding Path=str,Mode=TwoWay}" VerticalAlignment="Top" Width="120"/>
<Button x:Name="button" Content="Check" HorizontalAlignment="Left" Margin="10,67,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/>
</Grid>
Run Code Online (Sandbox Code Playgroud)
C#代码
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
str = "OK";
}
public string str { get; set; }
private void button_Click(object sender, RoutedEventArgs e)
{
Console.WriteLine(str);
}
}
Run Code Online (Sandbox Code Playgroud)
首先,文本框未显示“确定”,但为空白。然后,我在文本框中键入了不同的文本,例如:“ blablabla”,不带引号。然后,我单击按钮以检查我的str属性是否已更新。显然,str仍包含“确定”。
我在这里做错了什么?我想念什么使绑定工作?
问题是,您不绑定到 Window 的代码隐藏,而是绑定到 DataContext。
尝试这个:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new DC();
}
public class DC
{
public string str { get; set; }
public DC()
{
str = "OK";
}
}
}
Run Code Online (Sandbox Code Playgroud)
通常,您会有两个不同的文件,但为了测试,您可以在一个文件中完成。之后,您的 DC (DataContext) 应该实现 INotifyPropertyChanged 接口。
尝试找到一些关于 MVVM 的文章,例如http://www.codeproject.com/Articles/165368/WPF-MVVM-Quick-Start-Tutorial
作为WPF的新手,所有这些Binding和DataContext爵士乐都可能令人困惑。让我们先从绑定表达式开始...
<TextBox Text="{Binding Path=str, Mode=TwoWay}"/>
这是什么要说的是,你想你的Text属性绑定到无论DataContext的TextBox是。DataContext本质上是您TextBox从中获取数据的“东西” 。现在是麻烦了。DataContext如果未明确设置,则从可视树中“上方”的元素继承。在您的代码中,TextBox继承DataContext自Grid元素,而继承又继承DataContext自Window元素。看到DataContext未在您Window设置的DataContext属性中应用默认值,即null。在DataContext还没有在任何你的窗口的子元素,其中,通过继承,将设置的设置DataContext该窗口的所有儿童null。
重要的是要注意,您已Source在绑定表达式中省略了该属性。
<TextBox Text="{Binding Source=left_out, Path=str, Mode=TwoWay}"/>
忽略此属性后,DataContext由于上述原因,绑定的来源隐含为elements ,在这种情况下为null。基本上,您的表达式在这里所说的是您想将DataContext.strWPF解析为的文本属性绑定到null.str。
嗯不错。现在,我们如何将DataContext您的TextBox.Text绑定设置为该窗口的“隐藏代码”,以便获取该str属性?有几种方法可以做到这一点,但是出于我们的目的,我们将重点关注在TextBox.Text属性绑定中显式设置它。现在,存在三种不同的绑定“源”类型属性。“源”是我们希望控件/元素的绑定从中获取数据的位置。我们有Source,RelativeSource和ElementName。我们仅将重点放在ElementName这里,但是其他对于研究和理解至关重要。
因此,让我们命名Window元素,以便我们可以通过ElementName属性访问它。
<Window x:Class="WpfApplication1.MainWindow"
x:Name="_window"
...
Run Code Online (Sandbox Code Playgroud)
现在,我们可以ElementName在TextBox.Text绑定上设置属性以引用窗口。
<TextBox Text="{Binding ElementName=_window, Path=str, Mode=TwoWay}"/>
这意味着绑定_window.str在尝试解析其绑定时将寻找该属性。在这一点上,您可能仍然看不到您的str价值TextBox。这是因为它的值是InitializeComponent在窗口的构造函数中的方法之后设置的。此功能是第一次解析绑定的地方。如果要str在调用前设置的值InitializeComponent,则会在中看到该值TextBox。
这将我们带到依赖属性。现在,只需知道依赖项属性已内置了更改通知,您的绑定便需要该通知,因此它“知道”绑定的更改时间以及何时重新解析绑定值。是的,您可以INotifyPropertyChanged在后面的代码中使用,但是在这种情况下使用DependencyProperties有很好的论据,这只会在此点混淆问题。但是,这是必须理解的另一件事。
这是DependencyProperty您的str物业的代码。
public static readonly DependencyProperty StrProperty
= DependencyProperty.Register("Str", typeof(string), typeof(MainWindow),
new FrameworkPropertyMetadata(FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
public string Str
{
get{return (string)GetValue(StrProperty);}
set{SetValue(StrProperty,value);}
}
Run Code Online (Sandbox Code Playgroud)
现在,您可以像这样设置值,并通过绑定到反映该值TextBox。
public MainWindow()
{
InitializeComponent();
Str = "OK";
}
Run Code Online (Sandbox Code Playgroud)
在这一点上,一切都应该很好。我希望这会有所帮助。我花了一些时间才掌握WPF的功能。我的建议是,以尽可能多的,你可以在阅读DataContext,Binding以及DependencyProperty因为这些是WPF的核心。祝好运!