在Loaded事件中设置DataContext时无法获取绑定值

时间:2014-02-13 09:27:46

标签: c# wpf data-binding

我有一个显示可绑定文本的用户控件。如果我将此用户控件放在窗口中并在该窗口的Loaded事件中设置DataContext,则无法检索控件的Loaded事件中的文本。为什么呢?

如果我在MainWindow构造函数中设置DataContext,一切正常。

在Loaded事件中设置DataContext是不对的?

以下是我的示例代码:

<UserControl x:Class="UserControlBinding.TestControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         x:Name="thisControl"
         Loaded="OnLoaded"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
    <TextBlock Text="{Binding TextToShow, ElementName=thisControl}" />
</Grid>

    public partial class TestControl : UserControl
{
    public string TextToShow
    {
        get { return (string)GetValue(TextToShowProperty); }
        set { SetValue(TextToShowProperty, value); }
    }
    public static readonly DependencyProperty TextToShowProperty =
        DependencyProperty.Register("TextToShow", typeof(string), typeof(TestControl), new PropertyMetadata(null));

    public TestControl()
    {
        InitializeComponent();
    }

    private void OnLoaded(object sender, RoutedEventArgs e)
    {
        Debug.WriteLine("TestControl.OnLoaded: DataContext=" + DataContext);
        Debug.WriteLine("TestControl.OnLoaded: TextToShow=" + TextToShow);
    }
}


<Window x:Class="UserControlBinding.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:UserControlBinding"
    Loaded="OnLoaded"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <local:TestControl TextToShow="{Binding TextToBind}" />
</Grid>

    public partial class MainWindow : Window
{
    public string TextToBind
    {
        get { return (string)GetValue(TextToBindProperty); }
        set { SetValue(TextToBindProperty, value); }
    }
    public static readonly DependencyProperty TextToBindProperty =
        DependencyProperty.Register("TextToBind", typeof(string), typeof(MainWindow), new PropertyMetadata(null));


    public MainWindow()
    {
        InitializeComponent();
        //this.DataContext = this;  // if I do this it works
    }

    private void OnLoaded(object sender, RoutedEventArgs e)
    {
        Debug.WriteLine("+MainWindow.OnLoaded");
        TextToBind = "this is the text to bind";
        this.DataContext = this;
        Debug.WriteLine("-MainWindow.OnLoaded");
    }
}

调试输出是:

+MainWindow.OnLoaded
-MainWindow.OnLoaded
TestControl.OnLoaded: DataContext=UserControlBinding.MainWindow
TestControl.OnLoaded: TextToShow=

2 个答案:

答案 0 :(得分:1)

这里的问题是,当Loaded事件触发时,你只是不能依赖完成的绑定

如果您将PropertyChangedCallback附加到TextToShow属性,则可以看到在Loaded TestControl事件后LoadedMainWindow事件中设置DataContext时触发了它你的{{1}}。

答案 1 :(得分:0)

不需要在此设置DataContext,而不是BindingRelativeSource一起使用..

<TextBlock Text="{Binding TextToShow, RelativeSource="{RelativeSource FindAncestor, AncestorType=UserControl}}" />

<local:TestControl TextToShow="{Binding TextToBind, RelativeSource="{RelativeSource Self}}" />