Jaggu
Jaggu

Reputation: 6428

How can we set source of data binding to this in xaml?

How can I write this expression in xaml?

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

Please note that I don't want to set DataContext = this in xaml.cs. I want to set the Source to be this.

Thanks in advance :)

Edit:

This is my simple usercontrol:

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

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

The binding doesn't work.

Upvotes: 0

Views: 188

Answers (2)

Mike Post
Mike Post

Reputation: 6460

Try using "{Binding Logo, RelativeSource={RelativeSource Self}}" (converter omitted for clarity).

edit: Based on your updated code, you want:

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

Upvotes: 2

Leon K
Leon K

Reputation: 221

You should do this in the XAML:

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

and in your code (Constructor / UserControl_Loaded) you should do:

TextBlock1.DataContext = this;

Also you might need to Implement INotifyPropertyChanged and Raise OnPropertyChanged Event in the Property's Set

Why don't you want to do it in code?

Upvotes: 0

Related Questions