UserControl DataBinding属性不起作用

时间:2014-02-28 04:11:43

标签: c# wpf xaml data-binding user-controls

我有一个UserControl,它有一些我希望绑定到XAML的属性。

<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));
    }

}

我有一个包含UserControl的窗口,就像这样

<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

3 个答案:

答案 0 :(得分: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和BackgroundImage的依赖属性:

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); }
    }

答案 1 :(得分:0)

我觉得你没有在MainWindow中正确包含Usercontrol, 在MainWindow中包含以下命名空间

xmlns:local="clr-namespace:YourNameSpace"

并添加您的Usercontrol,如下所示:

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

答案 2 :(得分:0)

可能是datacontext问题,因为如果该节目为null,则上下文未正确设置