如何在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.";
}
}
}
結合沒有按」工作。
直到今天,我還以爲自我在綁定表達意味着控制本身。我不知道Self是指這個關鍵字。 – Jaggu 2012-01-16 04:03:28
它不工作。看到我更新的問題。 – Jaggu 2012-01-16 05:31:20
我是正確的自我意味着它會找到不屬於usercontrol的相同元素中的屬性。 – Jaggu 2012-01-16 05:40:52