2014-02-28 42 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,则上下文设置不正确