Skip to main content
Home Forums General Silverlight Getting Started New to databinding
8 replies. Latest Post by talldaniel on June 28, 2009.
(0)
talldaniel
Member
590 points
331 Posts
06-19-2009 4:58 PM |
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;
{
InitializeComponent();
buildData = myBuildData;
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
Participant
1028 points
337 Posts
06-19-2009 6:06 PM |
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}"
06-19-2009 6:57 PM |
That didnt do it, but thanks for the effort
clint1222
1542 points
216 Posts
06-19-2009 10:12 PM |
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.
06-27-2009 2:23 PM |
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
1042 points
300 Posts
06-27-2009 6:49 PM |
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.
06-27-2009 8:07 PM |
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?
06-28-2009 4:34 AM |
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:
06-28-2009 3:57 PM |
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