2014-02-28 55 views
0

我有一個UserControl,它具有一些我希望綁定到XAML的屬性。UserControl DataBinding屬性不起作用

<UserControl x:Class="UserControl1" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300" DataContext="{RelativeSource Self}"> 
<UserControl.Background> 
    <ImageBrush ImageSource="{Binding BackgroundImage}" Stretch="UniformToFill" AlignmentX="Center" AlignmentY="Bottom"/> 
</UserControl.Background> 

<Grid Name="mainGrid"> 
    <Label Canvas.ZIndex="-1" Foreground="Gray" Content="{Binding VersionNumber}" Height="28" HorizontalAlignment="Left" Name="versionLabel" VerticalAlignment="Bottom" /> 
</Grid> 
</UserControl> 

和代碼隱藏:

public partial class UserControl1 : UserControl, INotifyPropertyChanged 
{ 
    public string VersionNumber { private get; set; } 
    public ImageSource BackgroundImage { private get; set; } 

    public UserControl1() 
    { 
     InitializeComponent(); 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    protected virtual void OnPropertyChanged(string propertyName) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); 
    } 

} 

我有一個包含用戶控件窗口,像這樣

<Window x:Class="MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:SomeNameSpace" 

     Title="MainWindow" 
     MinHeight="400" MinWidth="400" > 
<local:UserControl1 BackgroundImage="images\background.png" VersionNumber="10"/> 

當然,實際的窗口不顯示任何東西,背景是空白的,Label.Content爲空,但Autos窗口顯示我屬性設置正確,像這樣。 Autos

我一直在搞這個過去2個小時左右,我不知道怎麼回事。

編輯 我已經試過這

private string versionNumber; 
public string VersionNumber { get { return this.versionNumber; } 
set { 
     this.versionNumber = value; 
     OnPropertyChanged("VersionNumber"); 
    } 
} 

,它仍然無法正常工作,在這種情況下,標籤不更新。

result

+0

你缺少OnPropertyChange在屬性中設置(「yourpropertyname」)。 –

+0

嘗試修改您的屬性二傳手公衆,並看看是否能解決問題 – har07

+1

@ har07二傳手是公開的,它只是是私有的,吸氣。 @MihaiHantea見的更新,它仍然沒有即使到呼叫工作'OnPropertyChanged()' – ron975

回答

1

您應該將其用於標籤綁定:

Content={Binding RelativeSource= {RelativeSource FindAncestor, AncestorType={x:Type wpfApplication1:UserControl1}}, Path=VersionNumber} 

UserControl1是您的用戶控件類的名稱。您必須指定名稱空間和用戶控件。

你可以讓你的私人獲得它仍然適用於此綁定。

編輯 按照你的代碼我做了這個演示:

//window.xaml

<Window x:Class="WpfApplication1.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:wpfApplication1="clr-namespace:WpfApplication1" 
    Title="MainWindow" Height="350" Width="525"> 
<Grid> 
    <wpfApplication1:UserControl1 VersionNumber="10"/> 
</Grid> 

//的UserControl1

<UserControl x:Class="WpfApplication1.UserControl1" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:wpfApplication1="clr-namespace:WpfApplication1" 
     mc:Ignorable="d" 
     d:DesignHeight="300" d:DesignWidth="300" DataContext="{RelativeSource Self}"> 
<UserControl.Background> 
    <ImageBrush ImageSource="{Binding BackgroundImage}" Stretch="UniformToFill" AlignmentX="Center" AlignmentY="Bottom"/> 
</UserControl.Background> 

<Grid Name="mainGrid"> 
    <Label Canvas.ZIndex="-1" Foreground="Gray" Content="{Binding RelativeSource= {RelativeSource FindAncestor, AncestorType={x:Type wpfApplication1:UserControl1}}, Path=VersionNumber}" 
      Height="28" HorizontalAlignment="Left" Name="versionLabel" VerticalAlignment="Bottom" /> 
</Grid> 

UserControl1。CS

public partial class UserControl1 : UserControl, INotifyPropertyChanged 
{ 
    private string _versionNumber; 
    private ImageSource _backgroundImage; 

    public UserControl1() 
    { 
     InitializeComponent(); 
    } 

    public string VersionNumber 
    { 
     private get { return _versionNumber; } 
     set 
     { 
      _versionNumber = value; 
      OnPropertyChanged("VersionNumber"); 
     } 
    } 

    public ImageSource BackgroundImage 
    { 
     get { return _backgroundImage; } 
     set 
     { 
      _backgroundImage = value; 
      OnPropertyChanged("BackgroundImage"); 
     } 
    } 


    public event PropertyChangedEventHandler PropertyChanged; 
    protected virtual void OnPropertyChanged(string propertyName) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 

由於@anees建議你也可以使用依賴屬性VERSIONNUMBER和背景圖片:

public static readonly DependencyProperty VersionNumberProperty = DependencyProperty.Register(
     "VersionNumber", typeof (string), typeof (UserControl1), new PropertyMetadata(default(string))); 

    public string VersionNumber 
    { 
     get { return (string) GetValue(VersionNumberProperty); } 
     set { SetValue(VersionNumberProperty, value); } 
    } 

public static readonly DependencyProperty BackgroundImageProperty = DependencyProperty.Register(
     "BackgroundImage", typeof (ImageSource), typeof (UserControl1), new PropertyMetadata(default(ImageSource))); 

    public ImageSource BackgroundImage 
    { 
     get { return (ImageSource) GetValue(BackgroundImageProperty); } 
     set { SetValue(BackgroundImageProperty, value); } 
    } 
+0

太棒了,這個功能非常好,謝謝! – ron975

0

我覺得你有沒有正確包括用戶控件在主窗口, 包括以下命名空間中的主窗口

xmlns:local="clr-namespace:YourNameSpace" 

並添加您的用戶控件類似如下:

<local:UserControl1 x:Name="somename" .... /> 
+0

似乎我忘記了以某種方式來說,我已經更新了問題以反映添加xmlns。數據綁定仍然不起作用。 – ron975

+0

您是否嘗試設置依賴項屬性? http://wpftutorial.net/DependencyProperties.html –

0

可能DataContext的問題,因爲如果這個節目的null,則上下文設置不正確