views:

45

answers:

2

Hi, I have a Usercontrol in a UserControl libracy (seperate assembly) i have in my xaml markup like this:

<UserControl x:Class="CenterTextTemplate.CenterTextTemplate"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
         Name="Test"
Height="Auto" Width="Auto">    
<Grid>
    <!--<TextBlock Name="TextField" Text="{Binding Text}"></TextBlock>      -->
    <Viewbox VerticalAlignment="Center" HorizontalAlignment="Center">
        <TextBlock  Name="TextField" Text="{Binding Text, ElementName=Test}" Foreground="Red" FontSize="50"></TextBlock>
    </Viewbox>
</Grid>

In my .cs file i have a property:

public string Text { get { return "test"; } }

When i load the usercontrol i see no "test" text... is there something im missing? Tried to not give a Name to the usercontrol but that didnt work either...

EDIT: In this setup i get this error: Error 1 The type name 'CenterTextTemplate' does not exist in the type 'CenterTextTemplate.CenterTextTemplate' C:\Documents and Settings\Brian Hvarregaard\My Documents\Visual Studio 2008\Projects\GreenWeb Templates\CenterTextTemplate\CenterTextTemplate.xaml 4 37 CenterTextTemplate

A: 

You need to use a dependency property to bind to a property. See this link as well as the msdn page for DependencyProperty.

Taylor Leese
But i only want to set the properties ONCE when i create an instance of the usercontrol - isnt dependencyproperties a bit overkill for that. All i want the textblock to do is take the text from my "Text" code behind property... thats it
H4mm3rHead
A: 

For Binding either you have to make it DependencyProperty or you have to implement INotifyPropertyChanged interface. If you don't ant to go either way, don't use binding, instead directly assign the values.

You can Bind with CLR property by using BindingMode=OneWayToSource

viky