C# 如何在xaml中设置数据绑定的源?

C# 如何在xaml中设置数据绑定的源?,c#,silverlight,silverlight-4.0,C#,Silverlight,Silverlight 4.0,如何用xaml编写这个表达式 Binding binding = new Binding("Logo"); binding.Source = this; binding.Converter = new ToImageConverter(); imgLogo.SetBinding(Image.SourceProperty, binding); 请注意,我不想在xaml.cs中设置DataContext=this。我想将源设

如何用xaml编写这个表达式

 Binding binding = new Binding("Logo");
            binding.Source = this;
            binding.Converter = new ToImageConverter();
            imgLogo.SetBinding(Image.SourceProperty, binding);
请注意,我不想在xaml.cs中设置
DataContext=this
。我想将源设置为这个

提前感谢:)

编辑:

这是我的简单用户控件:

<UserControl x:Class="SilverlightApplication4.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">
        <TextBlock Text="{Binding Tooltip, RelativeSource={RelativeSource Self}}"/>
    </Grid>
</UserControl>
绑定不起作用。

尝试使用“{binding Logo,RelativeSource={RelativeSource Self}}”(为清晰起见,省略了转换器)

编辑:
根据更新的代码,您需要:

<UserControl x:Class="SilverlightApplication4.MainPage"
    x:Name=MyUserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">
        <TextBlock Text="{Binding Tooltip, ElementName=MyUserControl}"/>
    </Grid>
</UserControl>

您应该在XAML中执行此操作:

<TextBlock Id="TextBlock1" Text="{Binding Tooltip}"/>
此外,您可能需要实现
INotifyPropertyChanged
并在属性集合中引发
OnPropertyChanged
事件


为什么不想在代码中实现呢?

直到今天,我还以为绑定表达式中的Self表示控件本身。我不知道Self引用了这个关键字。我是正确的Self意味着它将在同一个元素中找到属性,而不是在usercontrol中。根据更新的代码,您应该给usercontrol一个x:Name属性,然后使用ElementName绑定。因为我认为必须有一个XAML等价物。
<TextBlock Id="TextBlock1" Text="{Binding Tooltip}"/>
TextBlock1.DataContext = this;