2012-01-16 37 views
0

如何在xaml中編寫此表達式?我們如何在xaml中設置數據綁定源?

Binding binding = new Binding("Logo"); 
      binding.Source = this; 
      binding.Converter = new ToImageConverter(); 
      imgLogo.SetBinding(Image.SourceProperty, binding); 

請注意,我不想在xaml.cs中設置DataContext = this。我想將Source設置爲此。

感謝提前:)

編輯:

這是我簡單的用戶控件:

<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> 

這是我的CS:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 

namespace SilverlightApplication4 
{ 
    public partial class MainPage : UserControl 
    { 
     public static readonly DependencyProperty ToolTipProperty 
     = DependencyProperty.Register("ToolTip", typeof(string), typeof(MainPage), 
      new PropertyMetadata(string.Empty)); 

     public string Tooltip 
     { 
      get { return GetValue(ToolTipProperty) as string; } 
      set { SetValue(ToolTipProperty, value); } 
     } 

     public MainPage() 
     { 
      InitializeComponent(); 
      this.Loaded += new RoutedEventHandler(MainPage_Loaded); 
     } 

     void MainPage_Loaded(object sender, RoutedEventArgs e) 
     { 
      Tooltip = "Test tooltip."; 
     } 
    } 
} 

結合沒有按」工作。

回答

2

嘗試使用「{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> 
+0

直到今天,我還以爲自我在綁定表達意味着控制本身。我不知道Self是指這個關鍵字。 – Jaggu 2012-01-16 04:03:28

+0

它不工作。看到我更新的問題。 – Jaggu 2012-01-16 05:31:20

+0

我是正確的自我意味着它會找到不屬於usercontrol的相同元素中的屬性。 – Jaggu 2012-01-16 05:40:52

0

你應該在XAML做到這一點:

<TextBlock Id="TextBlock1" Text="{Binding Tooltip}"/> 

,並在你的代碼(構造函數/ UserControl_Loaded),你應該做的:

TextBlock1.DataContext = this; 

此外,您可能需要執行INotifyPropertyChanged and Raise OnPropertyChanged屬性集中的事件

爲什麼你不想在代碼中做到這一點?

+0

,因爲我認爲必須有一個XAML等價物。 – Jaggu 2012-01-17 03:52:02