English 中文(简体)
便于通过属性使用用户控制
原标题:Making an easy to use UserControl via Properties

在我的银光 4 应用程序中, 我试图创建一个简单的用户控制器, 由我的应用程序消耗。 为了保持简单, 它将有一个“ 标题 ” 和一个占位符, 我想设置任何类型的控制器 。

<User Control ...>
  <Grid x:Name="LayoutRoot">
    <TextBlock x:Name="TextBlockHeader" Text="{Binding Title}" />
    <ContentPresenter x:Name="ContentPresenterObject" />
  </Grid>
</UserControl>

在后面的代码中,我为TextBlock的文本创建了一个属性

public string Title
{
  get { return (string)GetValue(TitleProperty); }
  set { SetValue(TitleProperty, value); }
}

public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(MyAccordion), null);

这样,我就可以设定所有权财产, 当我在申请时使用控制中心时。

<local:MyAccordion Title="Test"/>

但似乎文本块 Text = "{ binding 标题}}" 上的绑定并没有使文本“ test” 显示为文本块文本 。

我的问题是:我怎样才能将产权标题显示为文本框文本? 如何将产权标题显示为可控制的任何类型的用户控制-conten control?

Thanks in advance,
Frank

最佳回答

答案是元素PropertyBinding 。 我需要在“ 绑定” 中引用用户控制, 或者在构建器中添加“ 绑定 ” 。

<强 > 在 XAML 中创建绑定 :

<User Control ... x:Name="userControl">
  ...
  <TextBlock x:Name="TextBlockHeader" Text="{Binding Title, ElementName=userControl}" />
</UserControl>

<坚固> 在构建器(后面的代码) 中创建绑定符( 后面的代码)

public MyUserControl()
{
  // Required to initialize variables
  InitializeComponent();

  TextBlockHeader.SetBinding(TextBlock.TextProperty, new System.Windows.Data.Binding() { Source = this, Path = new PropertyPath("Title") });
}

我还需要找出 如何增加一个儿童控制, 但这是另一个问题。

问题回答

也许控制或页面的数据链尚未设置 。 - 首先, 您应该阅读更多关于装订( http:// www. silverliight. net/ learn/ data- networking/ contact/ data- contactive- to- controls- (silverlight- quickstart) ) 的信息 。 如果您在实际工程上工作, 并且会设计某种电击, 您应该阅读 MVVM 模式 。





相关问题
Silverlight Rich text box control

Our team decided that we need our own custom Rich text box control for Silverlight app we are developing. We looked at existing controls mentioned at A good rich text control for Silverlight but ...

Silverlight ImageBrush not rendering (with Bing Map Control)

I m trying to add an image to a Pushpin instance from the Silverlight Bing Map Control, but I can t seem to get it to render (the pushpin renders fine). This is probably a general WPF question rather ...

Silverlight OpenFileDialog DoEvents equivalent

I m processing large files after they are selected by the user. My code looks like the following: if (FileDialog.ShowDialog() == true) { // process really big file } This freezes up the UI so ...

list of controls with templates in silverlight

Does anyone know where to find a list of controls that you can set the template on in Silverlight? I ve wasted several hours now trying to create control templates only to find that the control doesn ...

Silverlight, Updating the UI during processing

I have a simple silverlight multifile upload application, and i want to provide the user with some feedback, right now its only in a test phase and i dont have the webservice. Somehow i cant get the ...

Silverlight 3 - FindName returns null

This looks a bug to me.. Using Silverlight 3 and i have a user control defined in XAML and trying to access the object during runtime returns a null. <Grid> <common:CommonGridEditPanel x:...

silverlight 3 collection binding

Someone please help me understand why this binding does not work... I have a class called SelectionManager with a property called dates which is populated by a WCF service. The property is an ...

热门标签