【发布时间】:2009-12-18 20:01:24
【问题描述】:
我还有另一个 WPF 数据绑定问题...一个我在任何地方都没有找到答案的问题,这让我感到惊讶,因为它看起来非常基本。
基本上,我的代码中有一个字符串,我想在我的 GUI 中与文本框建立双向绑定。我认为在后面的代码中创建一个 DependencyProperty,然后通过 Source 绑定将其绑定到 TextBox 是一件简单的事情。问题是,我不能把一个或两个部分都弄好。
这是我在后面代码中的 DependencyProperty 定义:
public static readonly DependencyProperty FilePathProperty = DependencyProperty.Register( "FilePath", typeof(string), typeof(Window1));
public string FilePath
{
get { return (string)GetValue(FilePathProperty); }
set { SetValue( FilePathProperty, value); }
}
这是我的 XAML:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ReportingInterface Test Application" Height="300" Width="536">
<Menu DockPanel.Dock="Top">
<MenuItem Name="menu_plugins" Header="File">
<MenuItem Header="Open">
<StackPanel Orientation="Horizontal">
<Label>File location:</Label>
<TextBox Name="text_filepath" Width="100" Text="{Binding Source=FilePath, Path=FilePath, Mode=TwoWay}"></TextBox>
<Button Margin="3" Width="20">...</Button>
</StackPanel>
</MenuItem>
</MenuItem>
</Menu>
我知道显然是错误的部分是绑定部分...我讨厌在这里浪费人们的时间来回答这个问题,但老实说,我每次搜索都做不到(但至少现在此请求将填充后续的谷歌搜索)。 :)
谢谢!
【问题讨论】:
标签: wpf data-binding dependency-properties