1

我有以下控件嵌入(不是继承,只是放置): 在此处输入图像描述

Native TextBlock 放在 MyMiddleControl 中,MyMiddleControl 放在 MyGlobalControl 中。MyMiddleControl 有一个 DependencyPropery“GroupName”,它绑定到 TextBox.Text。MyGlobalControl 有一个公共属性“MyText,它绑定到 MyMiddleControl.GroupName。

XAML:

<UserControl x:Class="MyMiddleControl"
         ...
         DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}">
    <TextBlock Text="{Binding Path=GroupName}"/>
</UserControl>

public static readonly DependencyProperty GroupNameProperty =
    DependencyProperty.Register("GroupName", typeof(string), 
    typeof(MyMiddleControl), 
    new PropertyMetadata("default"));

public string GroupName
{
    get { return (string)GetValue(GroupNameProperty); }
    set { SetValue(GroupNameProperty, value); }
}

<UserControl x:Class="MyGlobalControl"
         ...
         DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}">
    <MyMiddleControl GroupName="{Binding MyText}"... />
</UserControl>

public string _myText = "myDef";
public string MyText
{
    get { return _myText ; }
    set { _myText = value; }
}

问题 #1。当我运行程序时,我在文本块中看到“默认”而不是“myDef”。

问题 #2。我有一个按钮:

private void TestButton_Click(object sender, RoutedEventArgs e)
{
    MyText = "TestClick";
}

结果是一样的:“默认”。

为什么?:(

更新:

我忘了说。如果我做

<UserControl x:Class="MyGlobalControl"
         ...
         DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}">
    <MyMiddleControl GroupName="FixedTest"... />
</UserControl>

它工作正常。

PS。与第一个答案不同的示例项目:TestBind.zip

4

1 回答 1

2

您必须实现 INotifyPropertyChanged 以通知绑定属性的更改

public class SampleData : INotifyPropertyChanged
{
  public SampleData ()
  {
     this.MyText = "myDef";
  }

  public string _myText;
  public string MyText
  {
    get { return _myText ; }
    set {
      _myText = value;
      this.RaisePropChanged("MyText");
    }
  }

  public event PropertyChangedEventHandler PropertyChanged;

  public void RaisePropChanged(string name) {
    var eh = this.PropertyChanged;
    if (eh != null) {
      eh(this, new PropertyChangedEventArgs(name));
    }
  }
}

希望有帮助

编辑 尝试以下解决方案,在后面的代码中设置 x:Name 和 DataContext

public partial class MyMiddleControl : UserControl
{
  public MyMiddleControl() {
    this.DataContext = this;
    this.InitializeComponent();
  }

  public static readonly DependencyProperty GroupNameProperty =
    DependencyProperty.Register("GroupName", typeof(string),
                                typeof(MyMiddleControl),
                                new PropertyMetadata("default"));

  public string GroupName {
    get { return (string)this.GetValue(GroupNameProperty); }
    set { this.SetValue(GroupNameProperty, value); }
  }
}

<UserControl x:Class="TestBind.MyMiddleControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             x:Name="middleControl">
    <Grid>
        <TextBlock Height="40" HorizontalAlignment="Left" Margin="45,23,0,0" 
                   Name="textBlock1" Text="{Binding ElementName=middleControl, Path=GroupName}" 
                   VerticalAlignment="Top" Width="203" />
    </Grid>
</UserControl>

public partial class MyGlobalControl : UserControl, INotifyPropertyChanged
{
  public MyGlobalControl() {
    this.DataContext = this;
    this.InitializeComponent();
    this.MyText = "myDef";
  }

  public string _myText;
  public string MyText {
    get { return this._myText; }
    set {
      this._myText = value;
      this.OnPropertyChanged("MyText");
    }
  }

  public event PropertyChangedEventHandler PropertyChanged;

  protected virtual void OnPropertyChanged(string propertyName) {
    PropertyChangedEventHandler handler = this.PropertyChanged;
    if (handler != null) {
      handler(this, new PropertyChangedEventArgs(propertyName));
    }
  }

  private void button1_Click(object sender, RoutedEventArgs e) {
    this.MyText = "btnClick";
  }
}

<UserControl x:Class="TestBind.MyGlobalControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:my="clr-namespace:TestBind"
             x:Name="globalControl">
  <Grid>
    <my:MyMiddleControl HorizontalAlignment="Left"
                        Margin="24,82,0,0"
                        x:Name="myFloatTest"
                        GroupName="{Binding ElementName=globalControl, Path=MyText}"
                        VerticalAlignment="Top" />
    <Button Content="Button"
            Height="23"
            HorizontalAlignment="Left"
            Margin="296,65,0,0"
            Name="button1"
            VerticalAlignment="Top"
            Width="75"
            Click="button1_Click" />
  </Grid>
</UserControl>
于 2012-08-31T16:26:01.693 回答