Begginer's issue with data binding
I'm following http://msdn.microsoft.com/en-us/library/ms752347.aspx to
learn how to use data binding and I don't understand it.
I've written the code almost exactly the same as the one in MSDN and it
looks like the application creates an instance of MyData object.
Then, how can I operate on this data?
Wouldn't it be better if I could use some already existing objects from
main program?
XAML code:
<DockPanel
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:SDKSample">
<DockPanel.Resources>
<c:MyData x:Key="myDataSource"/>
</DockPanel.Resources>
<DockPanel.DataContext>
<Binding Source="{StaticResource myDataSource}"/>
</DockPanel.DataContext>
<Label Height="39" HorizontalAlignment="Left" Margin="42,150,0,0"
Name="label1" VerticalAlignment="Top" Width="132" Content="{Binding
some_string}"/>
</DockPanel>
c# code:
namespace SDKSample
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
public class MyData
{
public string some_string { get; set; }
public MyData()
{
some_string = "my example string";
}
}
}
No comments:
Post a Comment