2009-09-30 126 views
0

我在UserControl库(单独的程序集)中有一个Usercontrol。我在我的XAML标记是这样的:如何将数据绑定到WPF Usercontrol中的属性?

<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> 

在我的.cs文件我有一个属性:

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

当我加载用户控件我没有看到“测试”文字。 ..有什么我失踪?试图给不给一个名称的用户控件,但没有工作要么...

编辑:

在此设置中我得到这个错误:

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

回答

0

您需要使用的依赖属性绑定到一个属性。请参阅link以及DependencyProperty的msdn页面。

+0

使用CLR属性绑定但我只希望当我创建的用户控件的一个实例设置一次属性 - 心不是dependencyproperties有点大材小用了点。我希望textblock所做的就是从属性背后的“文本”代码中获取文本......就是这样 – H4mm3rHead 2009-09-30 18:22:46

0

对于绑定,您必须将其设置为DependencyProperty,否则您必须实现INotifyPropertyChanged接口。如果您不想采取任何行动,请不要使用绑定,而应直接分配值。

您可以通过使用BindingMode = OneWayToSource

相关问题