wpf-dataBinding - set DataContext in xaml to an object in code

  • Thread starter Thread starter Rolf Welskes
  • Start date Start date
R

Rolf Welskes

Hello,
because there are no managed news group for window presentation foundation
and one
has told me I can use this, here my problem:

I have the following simple test:

<Window x:Class="Test02.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid Background="LightGray" Name="grid01">
<TextBlock Text="{Binding Path=Str01}" Background="Beige" Height="21"
Margin="36,32,122,0" Name="textBlock1" VerticalAlignment="Top" />
<TextBlock Text="{Binding Path=Str02}" Background="Beige" Height="21"
Margin="36,64,122,0" Name="textBlock2" VerticalAlignment="Top" />
</Grid>
</Window>

public partial class Window1 : Window
{

class ClsA
{
string str01;
string str02;

public ClsA(string str01, string str02) { this.str01 = str01;
this.str02 = str02; }

public string Str01
{
get { return str01; }
set { str01 = value; }
}

public string Str02
{
get { return str02; }
set { str02 = value; }
}
}

ClsA clsA = new ClsA("this ist string one", "this is string two");


public Window1()
{
InitializeComponent();

grid01.DataContext = clsA;
}
}


In this sample I set
grid01.DataContext = clsA in code.
I dont want this I want to set it in Xaml:

<Grid Background="LightGray" Name="grid01"
DataContext="---??????------">
So I can see the datacontext I use in xaml.
Further, I do NOT want to lay the ClsA means variable clsA in resources -
because ClsA , means clsA
can be very complex and need be generated by code.

So is there a solution, a syntactical possibility to write

<Grid Background="LightGray" Name="grid01"
DataContext="---??????------">
where ????? is in code not in xaml ?

Thank you for any help.
Best Regards
Rolf Welskes
 
Hi Rolf,

This is a quick note to let you know that I am performing research on this
issue and will get back to you as soon as possible. I appreciate your
patience.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Rolf,

I think you can create a public property in the window class to return the
instance of ClsA and you can use binding expression to bind it to the
Grid.DataContext:

<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300"
x:Name="myWin"<Grid Background="LightGray" Name="grid01">
<Grid.DataContext>
<Binding ElementName="myWin" Path="clsA" />
</Grid.DataContext>
<TextBlock Text="{Binding Path=Str01}" Background="Beige"
Height="21"
Margin="36,32,122,0" Name="textBlock1" VerticalAlignment="Top" />
<TextBlock Text="{Binding Path=Str02}" Background="Beige"
Height="21"
Margin="36,64,122,0" Name="textBlock2" VerticalAlignment="Top" />
</Grid>
</Window>


ClsA m_clsA = new ClsA("this ist string one", "this is string two");

public ClsA clsA
{
get { return m_clsA; }
set { m_clsA = value; }
}


I hope I didn't misunderstood your question.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hello,
thank you for you help, seems it works.
Best Regards
Rolf Welskes

"Walter Wang [MSFT]" said:
Hi Rolf,

I think you can create a public property in the window class to return the
instance of ClsA and you can use binding expression to bind it to the
Grid.DataContext:

<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300"
x:Name="myWin"<Grid Background="LightGray" Name="grid01">
<Grid.DataContext>
<Binding ElementName="myWin" Path="clsA" />
</Grid.DataContext>
<TextBlock Text="{Binding Path=Str01}" Background="Beige"
Height="21"
Margin="36,32,122,0" Name="textBlock1" VerticalAlignment="Top" />
<TextBlock Text="{Binding Path=Str02}" Background="Beige"
Height="21"
Margin="36,64,122,0" Name="textBlock2" VerticalAlignment="Top" />
</Grid>
</Window>


ClsA m_clsA = new ClsA("this ist string one", "this is string
two");

public ClsA clsA
{
get { return m_clsA; }
set { m_clsA = value; }
}


I hope I didn't misunderstood your question.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no
rights.
 
Back
Top