2012-09-19 76 views
2

这里给出的示例是我试图实现的实际UserControl的简化,但它说明了结构并遭受同样的问题。用户控件具有一个DependencyProperty Words,用于设置在用户控件XAML中定义的文本块的文本。衍生视图模型将UserControl属性绑定到INotifyPropertyChanged视图模型

public partial class MyControl : UserControl 
    { 
     public static readonly DependencyProperty WordsProperty = DependencyProperty.Register("Words", typeof(string), typeof(MyControl)); 
     public MyControl() 
     { 
      InitializeComponent(); 
     } 
     public string Words 
     { 
      get { return (string)GetValue(WordsProperty); } 
      set 
      { 
       m_TextBlock.Text = value; 
       SetValue(WordsProperty, value); 
      } 

一种INotifyPropertyChanged的ViewModelBase类被分配给主窗口的DataContext。 ModelText属性集调用OnPropertyChanged。

class MainWindow : ViewModelBase 
{ 
    private string m_ModelString; 
    public string ModelText 
    { 
     get { return m_ModelString; } 
     set 
     { 
      m_ModelString = value; 
      base.OnPropertyChanged("ModelText"); 
     } 
    } 
} 

在主窗口XAML绑定到用户控件和一个TextBlock

<Window x:Class="Binding.View.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="218" Width="266" xmlns:my="clr-namespace:Binding.View"> 
    <Grid> 
     <my:MyControl Words="{Binding ModelText}" HorizontalAlignment="Left" Margin="39,29,0,0" x:Name="myControl1" VerticalAlignment="Top" Height="69" Width="179" Background="#FF96FF96" /> 
     <TextBlock Height="21" HorizontalAlignment="Left" Margin="59,116,0,0" Name="textBlock1" Text="{Binding ModelText}" VerticalAlignment="Top" Width="104" Background="Yellow" /> 
    </Grid> 
</Window> 

的文本块结合的作品,但没有为用户控制方面。为什么不能像控制属性一样绑定UserControl DependencyProperty?

+0

您需要格式化与{} – Paparazzi

+0

的代码块我也尝试使用{}但很明显,我没有错。对不起,这是我第一次尝试。 – user1683083

回答

0

罪魁祸首是:

m_TextBlock.Text = value; 

WPF不直接使用由DP支持的特性。

如果要更新m_TextBlock.TextWordsProperty被修改,要么是正文块结合Words,或使用PropertyChangedCallbackUIPropertyMetadata

public static readonly DependencyProperty WordsProperty = 
     DependencyProperty.Register("Words", 
            typeof(string), 
            typeof(MyControl), 
            new UIPRopertyMetadata(
       new PropertyChangedCallback(
        (dpo, dpce) => 
        { 
         //Probably going to need to first cast dpo to MyControl 
         //And then assign its m_TextBlock property's text. 
         m_TextBlock.Text = dpce.NewValue as string; 
        }))); 

考虑dpo发件人,并dpce事件参数。

+0

谢谢我试了PropertyChangedCallback,它完美的作品。 – user1683083

+0

@ user1683083请考虑接受这个答案,然后;)(点击答案左上角的勾号) –

0

我认为你应该通过绑定而不是m_TextBlock.Text = value;将文本分配给你的UserControl的文本框。

也许你可以用户像这样在你的用户控件XAML绑定:

Text="{Binding Words 
, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" 
+0

在我给出的例子中,我可以看到你的观点。但是,我想要实现的实际用户控件更复杂。它使用画布通过平移和旋转变换在背景位图上渲染前景位图,但变换坐标来自模型。我计划向用户控件添加一个依赖项属性,以允许模型通过提供一个包含x,y,theta的类来通知用户控件,然后使用这些数据来修改转换,但是我无法使绑定生效。 – user1683083

+0

我认为更好的想法可能是将x,y,theta属性设置为用户控件中的DependencyProperties,然后在视图模型中为它​​们设置绑定。然后(就像狒狒说的),你可以订阅依赖属性的OnPropertyChange回调。 –

+0

我的想法是,你也可以在这个视图(xaml)中使用绑定到相同的用户控件的依赖属性。 –

相关问题