Skip to main content

Microsoft Silverlight

Answered Question New to databindingRSS Feed

(0)

talldaniel
talldaniel

Member

Member

590 points

331 Posts

New to databinding

I have been using Silverlight for a while, but i am new to databinding.  For some reason, it isnt working.  It just doesnt do anything.  can someone point out what I am donig wrong?  I am trying to bind to the local private variable buildData which has a member called "Environment"

 

Thanks,

 In my code i have

 BuildQueue buildData;

 

public viewBuildQueueItem(BuildQueue myBuildData)

{

// Required to initialize variables

InitializeComponent();

buildData = myBuildData;

this.DataContext = buildData;

 

init(buildData.SystemCode);

}

and in the XAML is

   <StackPanel Height="18" Margin="8,0,8,0" Orientation="Horizontal">
    <TextBlock Height="Auto" HorizontalAlignment="Left" Margin="0,0,0,0" Width="78" Text="Work order" TextWrapping="Wrap" VerticalAlignment="Top"/>
    <TextBlock HorizontalAlignment="Right" Margin="0,0,0,0" Width="Auto" Text="{Binding Environment, Mode=TwoWay}" TextWrapping="Wrap"/>
   </StackPanel>

K2P2
K2P2

Participant

Participant

1028 points

337 Posts

Re: New to databinding

I usually need to fight with this sort of binding myself.  But until someone who has more confidence answers it looks like you might need to add something:

 

talldaniel:
Text="{Binding Environment, Mode=TwoWay}"

Text="{Binding Path=Environment, Mode=TwoWay}"

talldaniel
talldaniel

Member

Member

590 points

331 Posts

Re: New to databinding

That didnt do it, but thanks for the effort

clint1222
clint1222

Participant

Participant

1542 points

216 Posts

Re: New to databinding

Hi,

The code you posted could work provided the code you didn't post is valid for Binding.

You're binding the Text of the TextBlock to the Environment property of BuildQueue.  Make sure the Environment property is marked public and is of type string (or a value converter exists to convert the Environment property type to a string).

You want to do two-way binding, so the BuildQueue class should implement the INotifyPropertyChanged interface.

 

talldaniel
talldaniel

Member

Member

590 points

331 Posts

Re: New to databinding

I thought that was going to work, and it didnt.  I am beginning to think there is  a bug in sl 3.0 or something, as this seems fairly straightforward, and yet, I have tried seemingly every possible permutation, and I cannot get it to bind, and no error is generated either.

Krasshirsch
Krasshirsch

Participant

Participant

1042 points

300 Posts

Re: New to databinding

Just tries a similar scenario, works perfectly, so there must be a mixup in your code, it would certainly help, if we'd see the hole control.

A Bro must always post bail for another Bro, unless it's out of state or, like, crazy expensive.

Crazy Expensive Bail > (Years You've been Bros) * $100

Alexander Wieser
Germany

clint1222
clint1222

Participant

Participant

1542 points

216 Posts

Answered Question

Re: New to databinding

Hi talldaniel,

Binding can be a little tricky sometimes.  And if something doesn't match up Silverlight just continues on as if the binding didn't exist.  No errors.  Here's what I have:

BuildData class:

 

using System.ComponentModel;

namespace YourProjectName
{
    public class BuildQueue : INotifyPropertyChanged
    {
        public BuildQueue()
        {
            _environment = "initial value";
        }
        private string _environment;
        public string Environment
        {
            get { return _environment; }
            set
            {
                _environment = value;
                PropertyChanged(this, new PropertyChangedEventArgs("Environment"));
            }
        }

        public void SetEnvironment()
        {
            Environment = "new value";
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}

The xaml:

        <StackPanel Height="25" Margin="8,0,8,0" Orientation="Horizontal">
            <TextBlock Height="Auto" HorizontalAlignment="Left" Margin="0,0,0,0" Width="78" Text="Work order" TextWrapping="Wrap" VerticalAlignment="Top"/>
            <TextBlock HorizontalAlignment="Right" Margin="0,0,0,0" Width="Auto" Text="{Binding Environment, Mode=TwoWay}" TextWrapping="Wrap"/>
            <Button x:Name="btnUpdate" Width="100" Height="25" Click="btnUpdate_Click"></Button>
        </StackPanel>

 The code behind:

using System.Windows;
using System.Windows.Controls;

namespace YourProjectName
{
    public partial class Binding : UserControl
    {
        BuildQueue bq = null;
        public Binding(BuildQueue buildData)
        {
            InitializeComponent();
            bq = buildData;
            this.DataContext = bq;
        }

        private void btnUpdate_Click(object sender, RoutedEventArgs e)
        {
            bq.SetEnvironment();
        }
    }
}

What's going on in the init(buildData.SystemCode); code?

 

Krasshirsch
Krasshirsch

Participant

Participant

1042 points

300 Posts

Re: New to databinding

clint1222:
And if something doesn't match up Silverlight just continues on as if the binding didn't exist.  No errors.  Here's what I have:
Thats not exactly true ;) Errors occur, check your output window, they just don't break the running code.

A Bro must always post bail for another Bro, unless it's out of state or, like, crazy expensive.

Crazy Expensive Bail > (Years You've been Bros) * $100

Alexander Wieser
Germany

talldaniel
talldaniel

Member

Member

590 points

331 Posts

Re: New to databinding

Clint,

Thanks for all the work on your example.  Miuch appreciated, and it got it going for me.

I am using SL 3.0 and Expression Blend 3.0 and I believe I am experiencing some bugs along with it now working.  In some circumstances it will recognize some keys and other keys are not recognized as having been pressed.  (when in the textbox that is two way bound)

 Anyway, I can live with the bug till the release date, and it mostly works now.  Thanks a lot

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities