开发者

Databinding issue with textbox and accessors

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; }
 }


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:

<Binding Path="JobKey" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged"/>

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:

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:c="clr-namespace:System.Workspace"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Grid.Resources>
        <c:Parameter x:Key="mySource"/>

        <DataTemplate x:Key="MyDataTemplate">
            <TextBlock Text="{Binding}"/>
        </DataTemplate>
    </Grid.Resources>

    <Canvas>
        <Canvas.DataContext>
            <Binding Source="{StaticResource mySource}" />
        </Canvas.DataContext>

        <ContentControl            
           Content="{Binding Path=JobKey}"             
           ContentTemplate="{StaticResource MyDataTemplate}" />
    </Canvas>

</Grid>

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.


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 a BasePar.


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.


Path="jobKey"

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜