2011-03-06 56 views
2

对于这个问题的目的范围内绑定控件的属性,我已经定义了一个非常简单的用户控制:用户控制

<UserControl x:Class="simpleUserControl.UserControl1" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      Height="300" 
      Width="300"> 
    <Grid> 
     <TextBox Name="TextBox1"/> 
    </Grid> 
</UserControl> 

我想(用户控件),用户能够设置“TextBox1中”的“文本”属性,所以我定义的属性(将其命名为“文”),其获取和设置TextBox1.Text:

namespace simpleUserControl 
{ 
    public partial class UserControl1 : UserControl 
    { 
     public string text 
     { 
      get { return TextBox1.Text; } 
      set { TextBox1.Text = value; } 
     } 

     public static readonly DependencyProperty textProperty = DependencyProperty.Register("text", typeof(string), typeof(UserControl1)); 

     public UserControl1() 
     {      
      InitializeComponent(); 
     } 
    } 
} 

现在,使用用户控制的时候,我想绑定这个'text'属性给一些字符串对象:

<Window x:Class="WpfApplication33.Window1" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:simple_user_control="clr-namespace:simpleUserControl;assembly=simpleUserControl" 
     Title="Window1" 
     Height="300" 
     Width="300" 
     Loaded="Window_Loaded"> 
    <Grid Name="MainGrid"> 
     <simple_user_control:UserControl1 Name="MyUserControl"> 
      <simple_user_control:UserControl1.text> 
       <Binding Path="my_text"/> 
      </simple_user_control:UserControl1.text> 
     </simple_user_control:UserControl1> 
    </Grid> 
</Window> 

和后面的代码:

namespace WpfApplication33 
{ 
    public partial class Window1 : Window 
    { 
     public Window1() 
     { 
      InitializeComponent(); 
     } 

     string my_text = "this is a text"; 
     private void Window_Loaded(object sender, RoutedEventArgs e) 
     { 
      MainGrid.DataContext = this; 
     } 
    } 
} 

但是,这并不出于某种原因...我不明白为什么,因为我已经设置了DataContext的,把一个参考用户控件。 ..我错了什么? (值得一提的是,直接设置“文本”属性的时候是这样的:

MyUserControl.text = "Another text"; 

一切工作正常,因此,我认为,这个问题有什么用结合做的)。

+0

这是因为DependencyObject textProperty不会自动继承其父控件的绑定上下文。因此你的代码:不会返回任何东西。我有同样的问题,仍然在寻找解决方案。 – 2013-01-08 21:03:16

回答

0

您没有财产,您有一个私人成员my_text,WPF不会绑定到它。

试试这个:

private string myText = "this is a text"; 
public string MyText 
{ 
    get 
    { 
     return myText; 
    } 

    set 
    { 
     myText = value; 
    } 
} 

而且你应该实现INotifyPropertyChanged在二传手,如果你想改变文本,并自动显示更改。

0

你说得对实施INotifyPropertyChanged,但仍有一些缺失。

主窗口的代码现在看起来像这样:

public partial class MainWindow : Window, INotifyPropertyChanged 
{ 
public event PropertyChangedEventHandler PropertyChanged; 

    private void OnPropertyChanged(String info) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(info)); 
     } 
    } 

    private string _my_text; 
    string my_text 
    { 
     get { return _my_text; } 
     set 
     { 
      _my_text = value; 
      OnPropertyChanged("my_text"); 
     } 
    } 

    private void Window_Loaded_1(object sender, RoutedEventArgs e) 
    { 
     MainGrid.DataContext = this; 
     MyUserControl.text = "This is a text"; 
     my_text = "Another text"; 
    } 
} 

然而,出现的唯一的文本是“这是一个文本”,而不是“其他文本”。

现在有什么问题?

0

您还没有将TextBox.Text属性连接到用户控件中的依赖项属性。 DependencyProperty文本中的更改将由WPF处理,并且实际上不会通过控件的文本属性。你应该定义你的控制如下的TextBox.Text属性绑定到你的依赖属性:

<UserControl x:Class="simpleUserControl.UserControl1" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Height="300" Width="300" x:Name="control"> 
<Grid> 
    <TextBox Name="TextBox1" Text="{Binding text, ElementName=control}"/> 
</Grid> 
</UserControl> 

在后台代码:

namespace simpleUserControl 
{ 
public partial class UserControl1 : UserControl 
{ 
    public string text 
    { 
     get { return (string)GetValue(textProperty); } 
     set { SetValue(textProperty, value); } 
    } 

    public static readonly DependencyProperty textProperty = DependencyProperty.Register("text", typeof(string), typeof(UserControl1)); 

    public UserControl1() 
    {      
     InitializeComponent(); 
    } 
} 
} 

然后在你的主窗口,你应该能够做到这一点,它应该工作:

private void Window_Loaded_1(object sender, RoutedEventArgs e) 
{ 
    MainGrid.DataContext = this; 
    my_text = "This is a text"; 
    my_text = "Another text"; 
}