文本框和访问器的数据绑定问题
我想知道我错过了什么?绑定根本不显示在文本框中。这些是我的代码:
XAML 命名空间:
xmlns:c="clr-namespace:mySystem.Workspace"
DataContext 和资源:
<Grid.Resources>
<c:Parameter x:Key="mySource"/>
</Grid.Resources>
<Canvas>
<Canvas.DataContext>
<Binding Source="{StaticResource mySource}" />
</Canvas.DataContext>
文本框:
<TextBox x:Name="TextBox" Width="159" Height="26" Canvas.Left="36" Canvas.Top="47">
<TextBox.Text>
<Binding Path="JobKey" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged"/>
</TextBox.Text>
类:
namespace mySystem.Workspace
{
public class Parameter : Object
{
访问器:
public BasePar JobKey
{
get { return jobKey; }
set { jobKey = value; }
}
I am wondering what am I missing? The binding is not displaying at all in the textbox. These are my codes:
XAML Namespace:
xmlns:c="clr-namespace:mySystem.Workspace"
DataContext and Resources:
<Grid.Resources>
<c:Parameter x:Key="mySource"/>
</Grid.Resources>
<Canvas>
<Canvas.DataContext>
<Binding Source="{StaticResource mySource}" />
</Canvas.DataContext>
Textbox:
<TextBox x:Name="TextBox" Width="159" Height="26" Canvas.Left="36" Canvas.Top="47">
<TextBox.Text>
<Binding Path="JobKey" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged"/>
</TextBox.Text>
The class:
namespace mySystem.Workspace
{
public class Parameter : Object
{
The accessors:
public BasePar JobKey
{
get { return jobKey; }
set { jobKey = value; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这里有很多奇怪的事情,但最明显的能让您工作的是绑定路径区分大小写。
将您的绑定更改为:
这应该使绑定正常工作。
我也不确定 BasePar 是什么类型,或者应该是什么类型,但除非你故意做一些聪明的事情,否则只需将其设置为像字符串这样的标准类型。
您可能也不应该使用名称空间
System.Workspace
,而是使用与您自己的项目相关的名称空间。在您的回复之后,我唯一能猜测到 BasePar 对象的用途是在 DataTemplate 中使用,比如在 ItemsControl 上。 DataTemplate 的行为是,当它们不知道如何呈现对象时,它们将回退到对象的 .ToString() 方法。
现在,在我的评论中,我说我不认为
TextBox
可以有 DataTemplate,我相信这是真的但是我确实在 这个 Stackoverflow 问题 它模板化了内容控件和文本块。代码如下:我现在没有时间让 TextBox 工作 - 考虑到我的前几次尝试,我什至不知道它是否可能。但是,这可能会帮助您到达您需要去的地方。
但仍然 - 如果我是我,我只会使用简单的绑定到标准对象。在这种情况下我看不出 BasePar 类的好处。
There are lots of odd things here but the most obvious one that will get you working is that the Binding Path is case sensitive.
Change your binding to:
This should get the binding working.
I'm also not sure what type BasePar is, or is meant to be, but unless you are doing something clever intentionally, just make it a standard type like string.
You should also probably not use the namespace
System.Workspace
, but something related to your own project.After your response, the only thing I can guess that the BasePar object is intended for, is to be used within a DataTemplate, on an ItemsControl say. DataTemplates have the behaviour that when they do not know how to render an Object they will fall back the the Object's .ToString() method.
Now, in my comment I said that I don't think the
TextBox
can have a DataTemplate, and I believe this is true however I did find a trick at this Stackoverflow question which templates a content control and a textblock instead. The code is below:I don't have time right now to get the TextBox working - don't even know if it is possible, given my first few tries. However, this might help get you where you need to go.
But still - if I was me I'd just use simple binding to standard objects. I can't see the benefit of the BasePar class in this scenario.
BasePar
实现是什么样的?查看“调试输出”窗口,看看是否有如下行:System.Windows.Data Error: 1 : Cannot create default converter to Perform 'two-way' conversions between types 'WpfApplication1.BasePar' 和'系统.字符串'。考虑使用 Binding 的 Converter 属性。 BindingExpression:Path=JobKey; DataItem='参数' (HashCode=14209755);目标元素是“TextBox”(名称=“TextBox”); target property is 'Text' (type 'String')
这告诉您,您正在尝试绑定到该属性,但 WPF 无法创建 2 向绑定,因为它无法转换文本(您键入TextBox) 到“BasePar”对象中。
根据 David 的建议,您可以绑定到原始字符串类型,或者(根据上面的警告消息)您可以向绑定添加一个
Converter
以将字符串转换为BasePar< /代码>。
What does the
BasePar
implementation look like? Have a look in the Debug Output window to see if you have a line like this:System.Windows.Data Error: 1 : Cannot create default converter to perform 'two-way' conversions between types 'WpfApplication1.BasePar' and 'System.String'. Consider using Converter property of Binding. BindingExpression:Path=JobKey; DataItem='Parameter' (HashCode=14209755); target element is 'TextBox' (Name='TextBox'); target property is 'Text' (type 'String')
This is telling you that you are trying to bind to the property, but WPF cannot create a 2-way binding, because it cannot convert the text (you type into the TextBox) into a 'BasePar' object.
As per David's suggestion, you could bind to a primitive string type, or alternately (as per the warning message above) you could add a
Converter
to the binding to convert a string into aBasePar
.您需要通过从 DependencyObject 或从 INotifyPropertyChanged 并添加所有通知代码等。
如果您不这样做,那么您将不会收到更新通知,并且您的绑定将无法按预期工作。
you need to make jobkey a DependencyProperty by deriving it from DependencyObject or derive your class from INotifyPropertyChanged and add all the notify code, etc.
if you do not do this, then you will not receive update notifications and your bindings wont work as expected.
您需要绑定到属性而不是字段,即使其大写。另外:要调试绑定,请检查 Visiual Studio 中的输出窗口。
You need to bind to the property not the field, i.e. make that upper-case. Also: To debug bindings check the Output-window in Visiual Studio.