[英]How to databind to a property in a WPF Usercontrol?
我在UserControl
庫(單獨的程序集)中有一個Usercontrol
。 我的XAML標記中有這樣的內容:
<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"; } }
加載用戶控件時,我看不到“測試”文本……我缺少什么嗎? 試圖不給用戶控件一個名字,但是那也不起作用...
編輯:
在這種設置下,我得到這個錯誤:
錯誤1類型名稱'CenterTextTemplate'在類型'CenterTextTemplate.CenterTextTemplate'中不存在C:\\ Documents and Settings \\ Brian Hvarregaard \\ My Documents \\ Visual Studio 2008 \\ Projects \\ GreenWeb Templates \\ CenterTextTemplate \\ CenterTextTemplate.xaml 4 37 CenterTextTemplate
您需要使用依賴項屬性來綁定到屬性。 請參閱此鏈接以及DependencyProperty
的msdn頁面。
對於綁定,您必須使其成為DependencyProperty或必須實現INotifyPropertyChanged接口。 如果您不選擇任何一種方式,請不要使用綁定,而應直接分配值。
您可以通過使用BindingMode = OneWayToSource與CLR屬性綁定
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.