2012-01-16 14 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を設定したくないことに注意してください。私はソースをこれに設定したい。事前:)で

おかげ

編集:

これは私の簡単なユーザーコントロールです:これは私のCSである

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

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."; 
     } 
    } 
} 

結合doesnの」仕事。

答えて

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

今日まで私はバインディング表現の自己がコントロールそのものを意味すると考えました。自己がこのキーワードを参照しているかどうかはわかりませんでした。 – Jaggu

+0

動作していません。更新された質問を参照してください。 – Jaggu

+0

私は正しかった自己は、usercontrolにない同じ要素内でプロパティを見つけることを意味します。 – Jaggu

0

あなたはXAMLでこれを実行する必要があります。

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

とあなたのコード(コンストラクタ/ UserControl_Loaded)あなたがやるべきで

TextBlock1.DataContext = this; 

INotifyPropertyChanged とRaise OnPropertyChangedプロパティのセットのイベント

なぜコードでやりたいのですか?

+0

私はそれに相当するXAMLがなければならないと考えているからです。 – Jaggu

関連する問題